如果城市中存在特定邻居,我想恢复一个布尔值
城市
City_Id | State_Id | CityName
1 6 São Paulo
2 22 São Paulo
3 26 São Paulo
邻居
Neighbor_Id | City_Id | NeighborName
询问
WITH cte
AS (
SELECT City.*
FROM City
WHERE (City.CityName COLLATE SQL_Latin1_General_CP1_CI_AI) LIKE 'são paulo'
)
SELECT cte.*
,CASE
WHEN EXISTS (
SELECT Neighbor.City_Id
FROM Neighbor
INNER JOIN cte ON Neighbor.City_Id = cte.city_id
WHERE (Neighbor.NeighborName COLLATE SQL_Latin1_General_CP1_CI_AI) LIKE 'ademar'
)
THEN 1
ELSE 0
END AS F
FROM cte
结果
City_Id | State_Id | CityName | f
1 6 São Paulo 1
2 22 São Paulo 1
3 26 São Paulo 1
错误
我知道在 City_Id 2 中,我的表中不存在任何具有此城市 ID 的邻居,那么为什么CASE EXISTS
在 City Id = 2 时在我的表中返回 1 ?