Try something like this:
SELECT id FROM test WHERE value in (2,5) GROUP by id HAVING count(*) = 2
If you want to test it, a simple table for it (no indexes!):
CREATE TABLE IF NOT EXISTS test ( id int(4) NOT NULL, `value` int(4) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO test (id, value) VALUES (100, 1), (100, 3), (101, 1), (101, 4), (102, 2), (102, 5), (103, 2), (103, 4), (104, 1), (104, 3), (105, 2), (105, 5);
I had a very similar question a few days ago. see MySQL - find rows matching all rows from a joined table
source share