2

我有两个要合并的查询,但我不知道如何合并。

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 行,但我也不知道该怎么做。

4

4 回答 4

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'
)
于 2012-11-26T15:22:21.793 回答
2

好吧,您可以直接使用连接谓词中的一系列ORs 进行连接,只要system_users 中的fathermotherguardian列实际上是s 即可。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';
于 2012-11-26T15:27:48.677 回答
0

你为什么不使用加入?

Select father, mother, guardian from students as s
join system_users usrs
on s.user_id=usrs.user_id
where s.user_id = '201209291';
于 2012-11-26T15:22:16.850 回答
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');

  • 我是否正确理解了这个问题?
于 2012-11-26T15:59:16.563 回答