EnexoOnoma
EnexoOnoma

Reputation: 8836

How to edit my mySQL query to select the users with this requirement?

I have the table below, and I want to select all the users that have a row with type_id equals to 3 and a row with type_id equals to 5.

I know I am wrong, but how shall I edit my query?

The table

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

The query

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

Upvotes: 0

Views: 34

Answers (2)

Strawberry
Strawberry

Reputation: 33935

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

Upvotes: 1

Syscall
Syscall

Reputation: 19779

You can join on the same table and check if type_id have the values you want:

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

Upvotes: 1

Related Questions