select gameid from ( select group_concat(type,"-",v) as final,gameid from (select gameid,type,v from ( select gameid,type, group_concat(value) as v from ( select * from table_name order by gameid asc,type asc,value asc ) as u where (type=1 and value in ('5','8')) or (type=2 and value in ('1')) group by gameid,type ) as tmp ) as t group by gameid ) as f where final='1-5,8,2-1'