Szymon Toda
Szymon Toda

Reputation: 4516

MySQL check if multiple rows exist

Here you can find how to check row existance:
SELECT EXISTS(SELECT 1 FROM table1 WHERE some_condition);

How to efficiently existance of multiple rows from table like:

SELECT EXISTS(SELECT 1 FROM table1 WHERE key = 0);
SELECT EXISTS(SELECT 1 FROM table1 WHERE key = 2);

from table:

key,username
0,foo
1,bar
2,boo

to return positive only if both rows (with key 0 and 2) was found?

Upvotes: 2

Views: 4554

Answers (2)

Akhil
Akhil

Reputation: 2602

SELECT count(distinct key) = 2  FROM table1 WHERE key in (0, 2) 

Upvotes: 4

juergen d
juergen d

Reputation: 204924

SELECT sum(`key` = 0) as key0_count,
       sum(`key` = 2) as key2_count 
FROM your_table

Upvotes: 1

Related Questions