我尝试在SQL Server 2012中运行以下说明:
SELECT ID, Title
from Movies
where categoryID = 1 and (MovieID,1) in
(select MovieID, count(ID)
from Copies
group by (MovieID))
但是抛出以下错误消息:
* 消息4145,第15级,状态1,第3行 在预期条件的上下文中指定的非布尔类型的表达式,靠近','。*
我知道这个查询在PostgreSQL中运行得很好。为了在SQL Server 2012中正常运行,我应该更改什么?
答案 0 :(得分:2)
您应该可以使用标准联接语法来执行此操作:
SELECT m.ID, m.Title
FROM Movies m
INNER JOIN (
SELECT c.MovieID FROM Copies c GROUP BY c.MovieID HAVING COUNT(*) = 1
) cc ON m.MovieID = cc.MovieID
WHERE m.categoryID = 1
答案 1 :(得分:0)
SELECT m.ID, m.Title
from Movies m
where m.categoryID = 1
and 1 = (select count(*)
from copies c
where c.MovieID = m.MovieID);