简体   繁体   English

SQL:指定列名称引用的表

[英]SQL: Specify Which Table A Column Name Is Referring To

I am selecting data from the same table in one SQL query and need to specify which table the particular column I am using refers to . 我在一个SQL查询中从同一个表中选择数据,并且需要指定我正在使用的特定列所引用的表

BASICALLY: x is ambiguous and I need to make it non-ambiguous. 基本上:x是模糊的,我需要使它不含糊。

For example I have... 比如我有......

     SELECT DISTINCT x,
     (SELECT x FROM Y WHERE x=x)
     FROM Y                 ^ ^
                            | |
                            | |_ [x referring to outer select statement]
    [x referring to inner] _| 
    [  select statement  ]

I need to somehow specify that the x on the right hand side is referring to the current value of x from the outer select statement . 我需要以某种方式指定右侧x指的是外部select语句中x的当前值。 Currently, it believes that it is referring to itself so it always returns true. 目前,它认为它指的是它自己总是回归真实。

you should use table aliases: 你应该使用表别名:

SELECT DISTINCT h.x,
     (SELECT x FROM Y as g WHERE g.x=h.x)
     FROM Y as h  

This way you can create temp table names and point to them, especially helpful for self-joins 这样您就可以创建临时表名并指向它们,这对自联接尤其有用

Can we with or without 'AS' in Sql server: 我们可以在Sql server中使用或不使用“AS”:

table_name AS table alias
table_name table_alias

More info 更多信息

Just use: 只需使用:

 SELECT DISTINCT yOut.x,
 (SELECT yIN.x FROM Y as yIN WHERE yIN.x=yOut.x) as someColName
 FROM Y as yOut                      ^    ^
                                     |    |
                                     |    |_ [x referring to outer select statement]
[is your table alias ] ______________| 
[from inner select]

我建议你使用别名来表明它。

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

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