mysql - 需要帮助使用 SQL 编写查询,除了

标签 mysql sql

我正在编写一个查询,该查询将显示慈善机构尚未捐赠的内容。这是到目前为止我的脚本:

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/

相关文章:

Java + JDBC : Prepared statement failing

mysql - 文件未写入 mySQL 备份 CRON

java - 使用带有 tomcat 的 c3p0 与 MySQL 的僵尸连接

sql - 使用数据过滤器从键中获取所有项目

sql - 我需要几个 DATEDIFF 查询

mysql - 如何在 MySQL 中强制执行唯一约束?

python - 多个 MySQL JOIN 和重复的单元格

php - 在 Mysql 数据库中添加新记录时触发 ajax

mysql - Golang Mysql connectex 套接字 block

带有数学运算的 MySQL 子查询