我想选择已注册参加某项活动的人数,该费用由signup_limit
提供,存储在activity_info
中,以及已注册该活动的人数,存储在signups
中。如果没有人注册,我希望它以注册数量计算为零,但是正如我所知,它会完全丢弃该行。我试图使用左外连接,但我得到相同的结果。有什么建议吗?
SELECT info.code, info.signup_limit - COUNT(*) AS swag
FROM activity_info AS info
LEFT OUTER JOIN signups ON info.code = signups.activity_code, variables
WHERE
info.code IN (
SELECT schedule.code FROM schedule,variables WHERE schedule.date = variables.week
) AND variables.week = signups.date
GROUP BY signups.activity_code
更新:感谢评论解决了这个问题。谢谢大家!
答案 0 :(得分:0)
SQL变量中间有一个单词。移除以保持良好状态:
SELECT info.code,
info.signup_limit - count(*) AS swag
FROM activity_info AS info
LEFT OUTER JOIN signups ON info.code = signups.activity_code
WHERE info.code IN
(SELECT schedule.code
FROM schedule
INNER JOIN variables ON schedule.date = variables.week)
AND variables.week = signups.date
GROUP BY signups.activity_code
答案 1 :(得分:0)
我想这就是你想要的:
SELECT
AI.code,
(
AI.signup_limit -
(
SELECT
COUNT(*)
FROM
signups AS S
WHERE
S.activity_code = AI.code
)
) AS swag
FROM
activity_info AS AI
WHERE
info.code IN
(
SELECT
schedule.code
FROM
schedule INNER JOIN variables ON schedule.date = variables.week
) AND
variables.week = signups.date