SQL - 从子查询返回字段

时间:2015-09-22 14:27:42

标签: sql sql-server join subquery

如何从ug1表中恢复第二个名称字段?

select ug.name, ug.email, ug
from user_group as ug
where ug.id in (SELECT gm.id_user FROM group_member as gm
                where id_group in (SELECT ug1.id from user_group as ug1
                                   where ug1.btype='g'
                                     and ug1.group_type_handle = 'leaversys'))

1 个答案:

答案 0 :(得分:2)

如果您需要字段,请使用join代替in

select ug.name, ug.email, . . .
from user_group ug join
     group_member gm
     on ug.id = gm.id_user join
     user_group ug2
     on gm.id_group = ug2.id 
where ug2.btype = 'g' and ug2.group_type_handle = 'leaversys';

这不完全相同,因为它可能导致多行。但是,我怀疑在你的情况下这不会是一个问题。

现在,您可以从三个表中包含所需的任何列。