mysql嵌套查询in和in

时间:2016-11-02 06:18:51

标签: mysql nested

尝试在另一个查询中进行查询选择(可能更多可能作为搜索具有所有这些特征但不确定语法的内容的方法。想要做类似这样的事情:

Select person_name 
from people 
where person_name in (select person_name 
                      from people 
                      where characteristic ='tall') and 
      in (select person_name 
                 from people 
                 where characteristic ='thin')

(找到同时又高又瘦的所有人的名字

2 个答案:

答案 0 :(得分:0)

您可以使用以下查询来获取具有这两个特征的人员:

select person_name 
from people 
where characteristic IN ('tall', 'thin')
group by person_name
having count(distinct characteristic) = 2

然后加入此子查询以获取其余字段:

select t1.*
from people t1
join (
   select person_name 
   from people 
   where characteristic IN ('tall', 'thin')
   group by person_name
   having count(distinct characteristic) = 2
) t2 on t1.person_name = t2.person_name 

答案 1 :(得分:0)

如果你想找到同时又高又瘦的所有人的名字,我想你只需要这个查询:

select person_name from people where characteristic in ('tall','thin')

只需设置特征 in ('tall','thin')

即可