我需要以某种方式检查查询结果是否是NULL
MySQL 返回错误Subquery returns more than 1 row
。请问我应该如何重写它才能正常工作?
SELECT DISTINCT user.id, user.username, user.name, user.surname, user.avatar,
(SELECT IF(ISNULL(
(SELECT attendant.fk_user AS u
FROM attendant
WHERE fk_event = 1
UNION
SELECT organizer.fk_user AS u
FROM organizer
WHERE fk_event = 1
UNION
SELECT invitation.fk_user_to AS u
FROM invitation
WHERE fk_event = 1)), NULL, 1)) AS related
...
将您的子查询选择更改为 count(*) 例如
或者更简单一点的是使用“WHERE EXISTS”。
有关详细信息,请参阅: https ://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
对于子查询使用
FROM (subquery1 union subqueryN) AS 相关
经过几个小时的尝试和谷歌搜索,我已经解决了它。这篇关于stackoverflow的帖子很有帮助。而不是
IF
andISNULL
there 应该CASE WHEN EXISTS
像这样使用: