这个查询替代加入(简单)工作吗?

时间:2010-07-05 14:45:44

标签: c# sql-server-2005 visual-studio-2008

链接是 http://www.sqlcommands.net/sql+join/

我想知道它是否会起作用

SELECT Weather.City
FROM Weather
WHERE Weather.City = State.City

意思是选择所有那些属于状态表的“天气”城市

如果不是,为什么会上述工作呢?

3 个答案:

答案 0 :(得分:2)

否,因为要使用State.City,表State需要位于FROM列表中的某个位置。

您提供的示例的替代方案是:

SELECT Weather.City
FROM Weather
INNER JOIN State
ON Weather.City = State.City

答案 1 :(得分:1)

您的查询将无效,因为表State未出现在FROM子句中,因此您无法引用其列。

这会有效:

SELECT Weather.City
FROM Weather
JOIN State
ON Weather.City = State.City

答案 2 :(得分:0)

或作为替代方案(使用英语句子作为指导):

SELECT City                -- select all those cities
FROM Weather               -- "from weather"
Where City In              -- which 
  (Select City From State) -- belong in the state table