我在触发器中有以下内容,并希望在返回NULL时插入零。我在过去两天在Stackoverflow和其他网站上看过各种帖子,但却无法实现我想要的......而且担心这可能不可能......?此查询返回MCVMoodle.mdl_facetoface_signups.userid中的所有用户,其中包含等级(满足WHERE条件)的计数值,计数值为1或更多,但没有0 / null。我理解使用没有结果/ null的困难,但是当计数为0 / null时,无论如何都要插入零?
INSERT INTO MCVMoodle.mdl_user_info_data_f2fskipcount (userid, fieldid, datab)
SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
FROM
MCVMoodle.mdl_facetoface_signups
LEFT JOIN
MCVMoodle.mdl_facetoface_signups_status on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid
WHERE
MCVMoodle.mdl_facetoface_signups_status.statuscode = 80
AND MCVMoodle.mdl_facetoface_signups_status.superceded = 0
AND MCVMoodle.mdl_facetoface_signups_status.grade = 0
GROUP BY
MCVMoodle.mdl_facetoface_signups.userid;
以下是最终为我工作的查询。感谢Gordon对此的重要帮助。
INSERT INTO MCVMoodle.mdl_user_info_data_f2fskipcount (userid, fieldid, datab)
SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
FROM MCVMoodle.mdl_facetoface_signups LEFT JOIN
MCVMoodle.mdl_facetoface_signups_status
on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid and
MCVMoodle.mdl_facetoface_signups_status.statuscode = 80 AND
MCVMoodle.mdl_facetoface_signups_status.superceded = 0 AND
MCVMoodle.mdl_facetoface_signups_status.grade = 0
GROUP BY MCVMoodle.mdl_facetoface_signups.userid
ON DUPLICATE KEY UPDATE
datab=VALUES(datab);
答案 0 :(得分:1)
如果我理解正确,您希望从查询中插入所有内容。但是如果查询为空,则插入所有NULL
的行。以下是这样做,但不是在MySQL中:
with cte as (
SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
FROM MCVMoodle.mdl_facetoface_signups LEFT JOIN
MCVMoodle.mdl_facetoface_signups_status
on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid
WHERE MCVMoodle.mdl_facetoface_signups_status.statuscode = 80 AND
MCVMoodle.mdl_facetoface_signups_status.superceded = 0 AND
MCVMoodle.mdl_facetoface_signups_status.grade = 0
GROUP BY MCVMoodle.mdl_facetoface_signups.userid
)
INSERT INTO MCVMoodle.mdl_user_info_data_f2fskipcount (userid, fieldid, datab)
select *
from cte
union all
select NULL, NULL, NULL
where not exists (select * from CTE);
MySQL版本更复杂:
INSERT INTO MCVMoodle.mdl_user_info_data_f2fskipcount (userid, fieldid, datab)
select *
from (SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
FROM MCVMoodle.mdl_facetoface_signups LEFT JOIN
MCVMoodle.mdl_facetoface_signups_status
on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid
WHERE MCVMoodle.mdl_facetoface_signups_status.statuscode = 80 AND
MCVMoodle.mdl_facetoface_signups_status.superceded = 0 AND
MCVMoodle.mdl_facetoface_signups_status.grade = 0
GROUP BY MCVMoodle.mdl_facetoface_signups.userid
) cte
union all
select NULL, NULL, NULL
where not exists (SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
FROM MCVMoodle.mdl_facetoface_signups LEFT JOIN
MCVMoodle.mdl_facetoface_signups_status
on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid
WHERE MCVMoodle.mdl_facetoface_signups_status.statuscode = 80 AND
MCVMoodle.mdl_facetoface_signups_status.superceded = 0 AND
MCVMoodle.mdl_facetoface_signups_status.grade = 0
GROUP BY MCVMoodle.mdl_facetoface_signups.userid
);
编辑:
我觉得您可能只想包含查询中的所有组合,甚至包括where
子句消除的组合。如果是,则将条件移至on
子句:
with cte as (
SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
FROM MCVMoodle.mdl_facetoface_signups LEFT JOIN
MCVMoodle.mdl_facetoface_signups_status
on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid and
MCVMoodle.mdl_facetoface_signups_status.statuscode = 80 AND
MCVMoodle.mdl_facetoface_signups_status.superceded = 0 AND
MCVMoodle.mdl_facetoface_signups_status.grade = 0
GROUP BY MCVMoodle.mdl_facetoface_signups.userid
)
INSERT INTO MCVMoodle.mdl_user_info_data_f2fskipcount (userid, fieldid, datab)
select *
from cte;