2

I have two queries that I want to join together, but I don't know how to.

Select father, mother, guardian from students where user_id = '201209291';

I get a result of 3 IDs.

Now, I need to use those three IDs to get their personal information;

Select * from system_users where user_id = (The 3 IDs from the previous query);

How would i go about solving this problem? I thought of the result of the first query into 3 rows, but I don't know how to do that either.

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

、、、および列が実際にsystem_usersにORある限り、結合述語で一連のsfatherを使用して直接結合できます。motherguardianuser_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 に答える