mysql对多表关联出来的数据如何去重?
table1:idnamemobile1Gosh111111112Sansy222222223Join33333333table2:idcitytable1_idwife...
table1:
id name mobile
1 Gosh 11111111
2 Sansy 22222222
3 Join 33333333
table2:
id city table1_id wife
1 beijing 3 a
2 beijing 3 b
3 xian 1 c
4 xian 1 d
5 shandong 2 e
6 shandong 2 f
关联这两张表,得到id,name,mobile,city:
select id,name,mobile,city from table1 left join table2 on table1.id = table2.table1_id;
结果:
id name mobile city
1 Gosh 11111111 xian
1 Gosh 11111111 xian
2 Sansy 22222222 shandong
2 Sansy 22222222 shandong
3 Join 33333333 beijing
3 Join 33333333 beijing
怎样写SQL让得出来的重复数据只保留一条有用信息呢?
其实只要最后group by id,name,mobile,city即可,group by有时候也可以起到去重的效果 展开
id name mobile
1 Gosh 11111111
2 Sansy 22222222
3 Join 33333333
table2:
id city table1_id wife
1 beijing 3 a
2 beijing 3 b
3 xian 1 c
4 xian 1 d
5 shandong 2 e
6 shandong 2 f
关联这两张表,得到id,name,mobile,city:
select id,name,mobile,city from table1 left join table2 on table1.id = table2.table1_id;
结果:
id name mobile city
1 Gosh 11111111 xian
1 Gosh 11111111 xian
2 Sansy 22222222 shandong
2 Sansy 22222222 shandong
3 Join 33333333 beijing
3 Join 33333333 beijing
怎样写SQL让得出来的重复数据只保留一条有用信息呢?
其实只要最后group by id,name,mobile,city即可,group by有时候也可以起到去重的效果 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询