group by - MySQL grouped query with OR & AND -


i have 2 tables, 1 data , 1 values data. need select data, have values that: (data=1 or data=2) , (data=3 or data=4)

for each info ofc there might many values should group by, that's thoughts this. i've tried this: select * t1 left join t2 on t1.id = t2.info_id val in (1) , val in (2) group id

of course doesn't work, because there no info different numbers in 1 field. help, please?

you can having clause:

select id t1 left join t2 on t1.id = t2.info_id group id having (sum(data = 1) > 0 or sum(data = 2) > 0) ,        (sum(data = 3) > 0 or sum(data = 4) > 0) 

each expression sum(data = 1) counts number of rows match value, within rows id same.

note: returns ids match condition. original data, need join tables.


Comments