mysql - SELECT multiple values to the same key in multiple tables -
i have 2 tables in following structure
table_1
uid | name | age 1 | john | 24 2 | adam | 35 3 | sara | 26
table_2
id | uid | meta_key | meta_value 1 | 2 | location | ny 2 | 2 | school | nyu 3 | 3 | location | ny 4 | 3 | school | xyz 6 | 1 | location | ny 6 | 1 | school | nyu
what trying select users table_1 location ny , school nyu
here query tried using no luck
select tabl_1.uid `tabl_1`, `tabl_2` tabl_1.uid = tabl_2.uid , table_2.meta_key in ('location', 'school') , table_2.meta_value in ('ny', 'nyu') order tabl_1.uid asc
i have looked everywhere without luck, if have query works or link solution appreciated, thank you.
you should try
select t1.uid tabl_1 t1 inner join tabl_2 t2 on t1.uid = t2.uid , t2.meta_key = 'location' , t2.meta_value = 'ny' inner join tabl_2 t3 on t1.uid = t3.uid , t3.meta_key = 'school' , t3.meta_value = 'nyu'
check result on http://sqlfiddle.com/#!2/f35ef/1/0
Comments
Post a Comment