简体   繁体   English

使用ON子句JOIN具有相同列名的表

[英]Using ON clause to JOIN tables with same column name

I am learning SQL and came across the following query. 我正在学习SQL并遇到以下问题。 I wanted to ask about the condition of "ON" clause while joining tables: 我想在连接表时询问“ON”子句的条件:

SELECT c_User.ID
FROM c_User
WHERE EXISTS (
    SELECT *
    FROM c_Group 
    JOIN c_Member ON (c_Group.Group_Name LIKE 'mcp%')
    WHERE 
        c_Group.Name = c_Member.Parent_Name
        AND c_Member.Child_Name = c_User.Lower_User_Name
)

I know that tables c_Member and c_Group have one column with the same name, Directory_ID . 我知道表c_Memberc_Group有一个名称相同的列Directory_ID What I expected was c_Member and c_Group to join on that column using something like: 我期望的是c_Memberc_Group使用类似的东西加入该列:

c_Group JOIN c_Member ON (c_Group.Directory_ID = c_Member.Directory_ID)
WHERE c_Group.Group_Name like 'mcp%'

Would someone explain how this condition is able to match the rows? 有人会解释这个条件如何能够匹配行吗?

c_Member ON (c_Group.Group_Name LIKE 'mcp%')
  1. Is this is shorter way of referring to two tables joining on a column with the same name, while applying the LIKE condition? 在应用LIKE条件时,这是引用两个连接具有相同名称的列的表的较短方式吗?
  2. If so, then can such style work for table with multiple column names that are the same? 如果是这样,那么这样的样式是否可以用于具有相同的多个列名的表?

Any thoughts or comments would be greatly appreciated. 任何想法或意见将不胜感激。 Thank you for your time. 感谢您的时间。

This is your correlated subquery: 这是您的相关子查询:

SELECT *
FROM c_Group 
JOIN c_Member ON (c_Group.Group_Name LIKE 'mcp%')
WHERE 
    c_Group.Name = c_Member.Parent_Name
    AND c_Member.Child_Name = c_User.Lower_User_Name

This subquery works, but the way it is spelled makes it quite unclear: 这个子查询有效,但它拼写的方式使得它很不清楚:

  • The join condition ( c_Group.Group_Name LIKE 'mcp%' ) is not actually not related to the table being joined ( c_Member ) ; 连接条件( c_Group.Group_Name LIKE 'mcp%' )实际上与正在连接的表无关( c_Member ); what it actually does is apply filter on table c_Group that makes it filter on (there is no magic such as shorter way of referring to two tables joining on a column with the same name, while applying the LIKE condition ). 它实际上做的是在表c_Group上应用过滤器,使其过滤(没有任何魔法,例如在应用LIKE条件时引用两个表加入具有相同名称的列的简短方法 )。 It would make more sense to move it to the WHERE clause (this would still be functionaly equivalent). 将它移动到WHERE子句会更有意义(这仍然是功能相当的)。

  • On the other hand, the WHERE clause contains conditions that relate to the tables being joined (for example: c_Group.Name = c_Member.Parent_Name ). 另一方面, WHERE子句包含与要连接的表相关的条件(例如: c_Group.Name = c_Member.Parent_Name )。 A more sensible option would be to put them in the ON clause of the JOIN . 一个更明智的选择是将它们放在JOINON子句中。

Other remarks: 其他评论:

  • when using NOT EXISTS , you usually would prefer SELECT 1 instead of SELECT * , (most RDBMS will optimize this under the hood for you, but this makes the intent clearer). 当使用NOT EXISTS ,你通常更喜欢SELECT 1而不是SELECT * ,(大多数RDBMS会为你优化这个,但这使得意图更清晰)。

  • table aliases can be used to make the query more readable 表别名可用于使查询更具可读性

I would suggest the following syntax for the query (which is basically syntaxically equivalent to the original, but a lot clearer): 我建议查询的以下语法(基本上在语法上等同于原始,但更清楚):

SELECT u.ID
FROM c_User u
WHERE EXISTS (
    SELECT 1
    FROM c_Group g 
    JOIN c_Member m ON g.Name = m.Parent_Name AND m.Child_Name = u.Lower_User_Name
    WHERE g.Group_Name LIKE 'mcp%'
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM