如何编辑mySQL查询以选择具有此要求的用户?

时间:2018-03-12 22:40:57

标签: php mysql

我有下表,我想选择所有用户,其中type_id等于3的行和type_id等于5的行。

我知道我错了,但我该怎样编辑我的查询?

表格

user --- type--- type_id
abc ---- form --- 3
abc ---- form --- 5
abc ---- form --- 6

查询

Select user from table where type = 'form' and type_id = 3 and type_id = 5

2 个答案:

答案 0 :(得分:1)

您可以加入同一张桌子并检查type_id是否具有您想要的值:

select t1.user 
from table t1
inner join on t1.user = t2.user and t2.type='form' and t2.type_id = 5
where t1.type = 'form' 
and t1.type_id = 3

答案 1 :(得分:1)

Select user 
  from table 
 where type = 'form' 
   and type_id IN(3, 5)
 Group
    By user
Having count(1) = 2;