我正在编写一个查询,该查询将显示慈善机构尚未捐赠的内容。这是到目前为止我的脚本:
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 All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists."
最佳答案
左连接
和where
怎么样:
SELECT c.Name
FROM CHARITY c LEFT JOIN
DONATION d
ON c.CharityID = d.DonationID
WHERE d.DonationID IS NULL;
我还要指出,将“CharityID”连接到“DonationID”的连接看起来非常可疑。
关于mysql - 需要帮助使用 SQL 编写查询,除了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47044684/