USING 关键字与 ON 子句 - MYSQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13750152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
USING Keyword vs ON clause - MYSQL
提问by Techie
Possible Duplicate:
MySQL ON vs USING?
可能的重复:
MySQL ON vs USING?
Query 1:
查询 1:
SELECT *
FROM users
JOIN orders ON (orders.user_id = users.user_id)
WHERE users.user_id = 1;
Query 2:
查询 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 ?
我想加入订单和用户表以获取某些数据。它工作正常。我的问题是因为两个查询输出相同的结果集,它是一样的吗?使用哪一种更有效?哪个对性能好?哪一个是最佳实践?
回答by echo_Me
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.
当我们从多个表中检索数据时,我们USING
不需要在JOIN
条件中提及该子句。当我们使用USING
子句时,该特定列名应该出现在两个表中,SELECT
查询将使用USING
子句中给定的列名自动连接这些表。
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
子句中提及所需的公共列名
USING
is also used while executing Dynamic SQL.
USING
在执行动态 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.
该
USING
子句:这允许你指定连接的名称键。该
ON
子句:此语法允许您加入两个表中的主键指定的列名。
The USING clause
USING 子句
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.
USING
如果多个列共享相同的名称,但您不想使用所有这些公共列进行连接,则使用该子句。USING 子句中列出的列在语句中不能有任何限定符,包括 WHERE 子句。
The ON clause
ON 子句
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.
该
ON
子句用于连接两个表中列名不匹配的表。连接条件从 WHERE 子句中的过滤条件中删除。