简体   繁体   English

Mysql左加入力分组依据

[英]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? 有没有一种方法可以使左联接返回没有重复的正确行?

depend on your data, you are probably having duplicated id on tablea so you are creating duplicates. 根据您的数据,您可能在tablea上具有重复的ID,因此您正在创建重复项。

without know your data you have two options 不知道您的数据有两种选择

SELECT *
FROM 
     (
      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. 第二个选项在每个子选择上使用group by

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM