Mysql Left加入Force Group By

时间:2022-10-18 20:11:45

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?

有没有办法让左连接返回正确的行而没有重复?

1 个解决方案

#1


0  

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而不是每个子选择。

#1


0  

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而不是每个子选择。