我需要找到有
的用户这是我提出的查询:
SELECT profile_id FROM productions_productionmember WHERE production_id in
(SELECT production_id FROM productions_productionmember WHERE profile_id=?)
UNION
SELECT profile_id FROM groups_groupmember WHERE group_id in
(SELECT group_id FROM groups_groupmember WHERE profile_id=?)
UNION
SELECT profile_id FROM videos_videocredit WHERE video_id in
(SELECT video_id FROM videos_videocredit WHERE profile_id=?)
相关表格:
groups_groupmember
- profile_id
- group_id
videos_videocredit
- profile_id
- video_id
productions_productionmember
- profile_id
- production_id
如何改进此查询?
答案 0 :(得分:0)
SELECT DISTINCT p.profile_id
FROM productions_productionmember p
left outer join groups_groupmember g on g.profile_id = p.profile_id
left outer join videos_videocredit v on v.profile_id = p.profile_id
WHERE v.video_id is not null or g.group_id is not null
or p.production_id in (SELECT production_id
FROM productions_productionmember
WHERE profile_id=?)
答案 1 :(得分:0)
Select *
From productions_productionmember as a,
groups_groupmember as b,
videos_videocredit as c
Where a.profile_id in (select ...) or
b.group_id in (select ...) or
c.video_id in (select...)
希望它有所帮助...
答案 2 :(得分:0)
您需要一个包含3个表格中任何一个的个人资料列表。
SELECT profile_id FROM productions_productionmember
UNION
SELECT profile_id FROM groups_groupmember
UNION
SELECT profile_id FROM videos_videocredit
如果production_id \ group_id \ _ video_id可以为null,则可以对这些查询使用IS NOT NULL检查。
您在查询中传递的profile_id是什么?如果你需要检查任何一个表中是否存在配置文件,那么你可以将where子句放在上面查询返回的结果上。
SELECT count(*)
from (SELECT profile_id FROM productions_productionmember
UNION
SELECT profile_id FROM groups_groupmember
UNION
SELECT profile_id FROM videos_videocredit ) subq
where subq.profile_id = ?
答案 3 :(得分:0)
SELECT pp.profile_id
FROM productions_productionmember pp
JOIN productions_productionmember pp2 ON pp.production_id=pp2.production_id
AND pp2.profile_id=?
UNION
SELECT gg.profile_id
FROM groups_groupmember gg
JOIN groups_groupmember gg2 ON gg.group_id=pp2.group_id
AND gg2.profile_id=?
UNION
SELECT vv.profile_id
FROM videos_videocredit vv
JOIN videos_videocredit vv2 ON vv.video_id=vv2.video_id
AND vv2.profile_id=?
这消除了内部查询。我不能告诉你这是否会提高查询速度 此查询返回profile_ids,可在表中找到两个或更多个。