我想进行查询以选择用户未注册的所有研讨会信息。我希望拥有1个查询的所有数据。
这是:研讨会表 :注册表 :workshop_rounds表
希望你们中的一些人可以帮助我。 提前谢谢!
我已经尝试过这个查询,但这不是我想要的,它是接近但不完全。
SELECT * FROM `workshop_rounds` WHERE `id` != ALL (SELECT `workshop_round_id` FROM `registrations` WHERE `user_id` = 1) AND `workshop_id` != ALL (SELECT `workshop_id` FROM `workshop_rounds` WHERE `workshop_id`= ALL (SELECT `workshop_round_id` FROM `registrations` WHERE `user_id` = 1))
但是现在我也希望不要使用与用户注册的轮次相同的workshop_id进行其他轮次。希望你能理解我。
答案 0 :(得分:1)
对于单个用户,我会使用not exists
:
select w.*
from workshops w
where not exists (select 1
from registrations r
where r.workshop_id = w.id and
r.user_id = THE USER ID
);
答案 1 :(得分:1)
使用NOT EXISTS
SELECT *
FROM workshops w
WHERE NOT EXISTS
(
SELECT 1
FROM registrations r
JOIN "workshop_rounds" wr ON r."workshop_round_id" = wr.id
WHERE wr.workshop_id = w.id
AND r."user_id" = 1
);
也许更简洁NOT IN
SELECT *
FROM workshops w
WHERE w.id NOT IN
(
SELECT wr.workshop_id
FROM registrations r
JOIN "workshop_rounds" wr ON r."workshop_round_id" = wr.id
WHERE r."user_id" = 1
);
答案 2 :(得分:0)
尝试使用此功能来获取用户未注册的所有研讨会
select ws.* from workshops ws
where ws.id not in (select w.id from workshops w, workshop_rounds wr, registrations r
where r.workshop_round_id = wr.id and wr.workshop_id=w.id and r.user_id = THE_USER_ID);