SQL - 2个不同写入的相等查询的工作方式不同

时间:2014-05-26 10:21:10

标签: sql linq-to-entities

以下查询1可以写为查询2吗?如果是,他们的工作方式不同。 如果没有,那么我需要查询2的LINQ版本。

查询1:

select c.Name as 'Country', s.Name as 'State', city.Name as 'City' 
from CountryRegion c,CountryRegion s,CountryRegion city 
where  city.Id=11 and s.Id=city.CountryRegionParentId and c.Id=s.CountryRegionParentId

查询2:

select c.Name as 'Country', s.Name as 'State', city.Name as 'City' 
from CountryRegion as c inner join CountryRegion as s on c.Id=s.Id
inner join CountryRegion as city on s.Id=c.Id
where  city.Id=11 and s.Id=city.CountryRegionParentId and c.Id=s.CountryRegionParentId

1 个答案:

答案 0 :(得分:0)

在查询2中似乎存在错误,因为您使用的是不同的连接条件,而不是where子句中的条件(这没有用)。如果您将查询2重写为以下内容,则它应完全等同于查询1:

select c.Name as 'Country', s.Name as 'State', city.Name as 'City' 
from 
    CountryRegion as city inner join 
    CountryRegion as s on s.Id = city.CountryRegionParentId inner join
    CountryRegion as c on c.Id = s.CountryRegionParentId
where  city.Id=11