深入理解mysql的on关键字_使用关键字vs ON子句-MYSQL

bd96500e110b49cbb3cd949968f18be7.png

Possible Duplicate:

MySQL ON vs USING?

Query 1:

SELECT *

FROM users

JOIN orders ON (orders.user_id = users.user_id)

WHERE users.user_id = 1;

Query 2:

SELECT *

FROM users

JOIN orders USING (user_id)

WHERE user_id = 1;

I want to join orders and users tables to get some certain data. It works fine. My issue is since both queries output the same results set, is it the same? Which one is more efficient to be used? Which one is good for performance ? Which one is the best practise ?

解决方案

The USING clause is something we don't need to mention in the JOIN condition when we are retrieving data from multiple tables. When we use USING clause, that particular column name should be present in both tables, and the SELECT query will automatically join those tables using the given column name in USING clause.

for e.g. if there are two common column names in the table, then Mention the desired common column name in the USING clause

USING is also used while executing Dynamic SQL.

e.g.,

EXECUTE IMMEDIATE 'DELETE FROM dept WHERE deptno = :num'

USING dept_id;

The USING clause: This allows you to specify the join key by name.

The ON clause: This syntax allows you to specify the column names for join keys in both tables.

The USING clause

The USING clause is used if several columns share the same name but you don’t want to join using all of these common columns. The columns listed in the USING clause can’t have any qualifiers in the statement, including the WHERE clause.

The ON clause

The ON clause is used to join tables where the column names don’t match in both tables. The join conditions are removed from the filter conditions in the WHERE clause.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值