我有两个想要加入的查询,但我不知道如何。
Select father, mother, guardian from students where user_id = '201209291';
我得到了3个ID的结果。
现在,我需要使用这三个ID来获取他们的个人信息;
Select * from system_users where user_id = (The 3 IDs from the previous query);
我将如何解决这个问题?我想到了第一个查询到3行的结果,但我不知道该怎么做。
答案 0 :(得分:3)
Select * from system_users where user_id IN
(
Select father from students where user_id = '201209291'
UNION
Select mother from students where user_id = '201209291'
UNION
Select guardian from students where user_id = '201209291'
)
答案 1 :(得分:2)
好吧,只要实际OR
,father
和mother
列guardian
,user_id
和select usrs.*
from students s
join system_users usrs on
s.father=usrs.user_id OR
s.mother=usrs.user_id OR
s.guardian=usrs.user_id
where s.user_id = '201209291';
列,您就可以直接使用连接谓词中的一系列{{1}}加入{1}}在system_users中。
{{1}}
答案 2 :(得分:0)
为什么不使用join?
Select father, mother, guardian from students as s
join system_users usrs
on s.user_id=usrs.user_id
where s.user_id = '201209291';
答案 3 :(得分:0)
从第一个查询中,您将没有ID但只有3列。
Select father, mother, guardian from students where user_id = '201209291'
。
可能你的意思是
Select distinct some_id from students where user_id = '201209291'
我建议简单:
Select * from system_users where user_id in (Select distinct some_id from students where user_id = '201209291');