我正在编写一个查询,显示慈善机构没有捐赠的内容。到目前为止,这是我的脚本:
SELECT c.Name
FROM CHARITY c INNER JOIN
DONATION d
ON c.CharityID = d.DonationID
EXCEPT
select C.CharityID, donationID
from charity c join
DONATION d
on c.CharityID = d.DonationID
ORDER BY d.DonationID asc
我的输出应说明:
name DonationID
------ ------
St. Francis Home NULL
Salvation Army Null
.....
.....
但我得到了:
“Msg 205,Level 16,State 1,Line 1 使用UNION,INTERSECT或EXCEPT运算符组合的所有查询在其目标列表中必须具有相同数量的表达式。“
答案 0 :(得分:1)
left join
和where
:
SELECT c.Name
FROM CHARITY c LEFT JOIN
DONATION d
ON c.CharityID = d.DonationID
WHERE d.DonationID IS NULL;
我也会指出连接" CharityID" to" DonationID"看起来真的很可疑。