Home Ask Login Register

Developers Planet

Your answer is one click away!

paola jamira February 2016

Mysql Left Join Force Group By

I have notice that when I use a left join to another table, My data duplicates itself and I don't have more option than make a group by so that I could have the correct rows.

SELECT * from tbla a left join tblb b on a.id=b.id_a
group by a.id

Are there a way to make a left join returning correct rows with no duplicate?


Juan Carlos Oropeza February 2016

depend on your data, you are probably having duplicated id on tablea so you are creating duplicates.

without know your data you have two options

      SELECT DISTINCT <tablA_fields> 
      from tbla a 
     ) as A
left join      (
      SELECT DISTINCT <tablB_fields> 
      from tblb b
     ) as B
tblb b on A.id = B.id_a

Second option use group by instead on each sub select.

Post Status

Asked in February 2016
Viewed 1,123 times
Voted 7
Answered 1 times


Leave an answer

Quote of the day: live life