我有3个表,t1包含事件,t2包含“有人参与”事件,t3包含一些受邀用户ID(链接到系统用户ID)。不止一个用户可以参加和活动。
创建新事件时,它会插入到t1中。每次用户确认参加事件时,在t2中插入一行,否则t2表为空。
下面的查询会提取用户正在参加的所有事件。
SELECT * FROM events t1
INNER JOIN attending_events t2 ON t1.id_ev = t2.id_ev
INNER JOIN attending_users t3 ON t2.id_user = t3.id_user AND t3.id_system_user=17
WHERE t1.id_comp = 42
请帮助,我需要一个查询来完全相反,找到用户没有参加的事件。
答案 0 :(得分:3)
SELECT e.*
FROM events e
WHERE e.id_ev NOT IN
(
SELECT id_ev
FROM attending_events ae
JOIN attending_users au
ON au.id_user = ae.id_user
WHERE au.system_user_id = 17
)
AND id_comp = 42
或:
SELECT *
FROM events e
LEFT JOIN
(
attending_events ae
JOIN attending_users au
ON au.id_user = ae.id_user
)
ON ae.id_ev = e.id_ev
AND au.system_user_id = 17
WHERE e.id_comp = 42
AND ae.id_ev IS NULL
答案 1 :(得分:0)
select * from attending_events where id_ev not in
(SELECT id_ev FROM events t1
INNER JOIN attending_events t2 ON t1.id_ev = t2.id_ev
INNER JOIN attending_users t3 ON t2.id_user = t3.id_user AND t3.id_system_user=17
WHERE t1.id_comp = 42)
我想到了第一件事。绝对应该优化。