php - MySQL join query but dont show results from the first table if it's a duplicate -
so have 2 tables:
table1
id| name 1 test 2 3 more
table2
id| table1_id 1 1 2 1 3 1 4 2 5 2 6 3
now need result this:
name | table2.id test 1 2 3 4 5 more 6
so no duplicate entries first table. exact same results joining without showing name more once. possible in mysql or should filter in php? know it's possible in php wondering if achievable in mysql if so, i'd know for. thinking distinct and/or left or right join.
so, asked if possible mysql , answered in comments is. if question how can accomplish mysql, here is:
select tmp.name, tbl2.id tbl2 left join ( select tbl2.id id, tbl1.`name` name tbl2 inner join tbl1 on tbl1.id = tbl2.tbl1_id group tbl2.tbl1_id ) tmp on tbl2.id = tmp.id;
hope wanted.
as @roberto06 suggested, query returns null
instead of duplicates, if don't null
s , want empty string can change select tmp.name
select ifnull(tmp.name,'')
Comments
Post a Comment