繁体   English   中英

SQL-2个不同编写的相等查询无法正常工作

[英]SQL - 2 equal queries differently written are not working the same

可以将以下查询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

查询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

暂无
暂无

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

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