3

我有 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

请帮忙,我需要一个查询来做完全相反的事情,找到用户没有参加的活动。

4

2 回答 2

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
于 2012-06-01T11:38:38.723 回答
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)

我首先想到的是。绝对应该优化。

于 2012-06-01T11:38:20.227 回答