mysql联表查询出现重复数据怎么解决

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql联表查询出现重复数据怎么解决相关的知识,希望对你有一定的参考价值。

子表(货物表)

主表(订单表)

查询订单号(g_num)时输出主表的订单和字表的属于这个订单的货物。
我查询的时候主表总是出来很多重复的,这是怎么回事啊使用group by 查询就会两张表都只出来一条数据了

下面是我写的SQL语句,请大神帮忙看看怎么解决啊?

参考技术A 明显是一对多吗

select * from 主 a left join 子 b on a.g_num=b.g_num追问

额我知道是一对多啊,这样查出来有重复啊,要怎么解决重复问题

mysql多表查询出现重复数据

CREATE
VIEW `NewView`AS
select distinct client.name,client.lxdh,client.yhzh,client.gzdw,client.jtzz,client.sfz ,jx.sfz,jx.cllx,jx.cph,jx.fdjh,jx.jqxh,jx.gmfs,jx.gmrq,jx.jqjg,jx.xsdw,bd.cph,bd.bxdh,bd.xz,bd.je,bd.ccs,bd.cbgs,bd.dysyr,bd.qbrq,bd.zbrq ,bd.username,bd.addtime ,bd.beiz,bd.bdtype from client,jx,bd where client.sfz=jx.sfz and jx.cph=bd.cph ;会重复出现三条数据,这是怎么回事,怎么解决呢?

我也遇到这样的问题,后来发现是写错了。
错误查询语句

select * from (#@__archives inner join dede_addonshop on #@__archives.typeid=dede_addonshop.typeid) where #@__archives.channel=6 ORDER BY RAND() limit 12
结果是:
运行SQL:select * from (#@__archives inner join dede_addonshop on
#@__archives.typeid=dede_addonshop.typeid) where #@__archives.channel=6
ORDER BY RAND() limit 12,共有9条记录,最大返回100条!记录:1id:4
typeid:6
typeid2:0
sortrank:1370428324
flag:p,a
ismake:1
channel:6
arcrank:0
click:106
money:0
title:3新款 情趣内衣套装制服 女式性感透明诱惑薄纱 齐B紧身睡裙8753
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130605/1-1306051S2150-L.jpg
pubdate:1370428324
senddate:1370428366
mid:1
keywords:齐,薄纱,B,紧身,8753,睡裙,诱惑,透明,套装,情趣内
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:情趣内衣套装制服 女式性感透明诱惑薄
filename:
dutyadmin:1
tackid:0
mtype:0
weight:3
aid:4
body:
3情趣内衣套装制服 女式性感透明诱惑薄

price:99
trueprice:44
brand:黑爵
units:
templet:
userip:127.0.0.1
redirecturl:
iid:11
记录:2id:6
typeid:6
typeid2:0
sortrank:1370575548
flag:p
ismake:1
channel:6
arcrank:0
click:175
money:0
title:2蝶华正品精致印花蕾丝花边女式聚拢型文胸套装
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130607/1-13060G126070-L.jpg
pubdate:1370575548
senddate:1370575620
mid:1
keywords:女式,聚拢型,文胸,套装,花边,蕾丝,正品,精致,印花,蝶华
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:蝶华正品精致印花蕾丝花边女式聚拢型文胸套装
filename:
dutyadmin:1
tackid:0
mtype:0
weight:5
aid:7
body:
测试用的哦1

price:2022
trueprice:1002
brand:美人儿
units:
templet:
userip:127.0.0.1
redirecturl:
iid:24418612187
记录:3id:6
typeid:6
typeid2:0
sortrank:1370575548
flag:p
ismake:1
channel:6
arcrank:0
click:175
money:0
title:2蝶华正品精致印花蕾丝花边女式聚拢型文胸套装
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130607/1-13060G126070-L.jpg
pubdate:1370575548
senddate:1370575620
mid:1
keywords:女式,聚拢型,文胸,套装,花边,蕾丝,正品,精致,印花,蝶华
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:蝶华正品精致印花蕾丝花边女式聚拢型文胸套装
filename:
dutyadmin:1
tackid:0
mtype:0
weight:5
aid:4
body:
3情趣内衣套装制服 女式性感透明诱惑薄

price:99
trueprice:44
brand:黑爵
units:
templet:
userip:127.0.0.1
redirecturl:
iid:11
记录:4id:4
typeid:6
typeid2:0
sortrank:1370428324
flag:p,a
ismake:1
channel:6
arcrank:0
click:106
money:0
title:3新款 情趣内衣套装制服 女式性感透明诱惑薄纱 齐B紧身睡裙8753
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130605/1-1306051S2150-L.jpg
pubdate:1370428324
senddate:1370428366
mid:1
keywords:齐,薄纱,B,紧身,8753,睡裙,诱惑,透明,套装,情趣内
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:情趣内衣套装制服 女式性感透明诱惑薄
filename:
dutyadmin:1
tackid:0
mtype:0
weight:3
aid:7
body:
测试用的哦1

price:2022
trueprice:1002
brand:美人儿
units:
templet:
userip:127.0.0.1
redirecturl:
iid:24418612187
记录:5id:6
typeid:6
typeid2:0
sortrank:1370575548
flag:p
ismake:1
channel:6
arcrank:0
click:175
money:0
title:2蝶华正品精致印花蕾丝花边女式聚拢型文胸套装
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130607/1-13060G126070-L.jpg
pubdate:1370575548
senddate:1370575620
mid:1
keywords:女式,聚拢型,文胸,套装,花边,蕾丝,正品,精致,印花,蝶华
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:蝶华正品精致印花蕾丝花边女式聚拢型文胸套装
filename:
dutyadmin:1
tackid:0
mtype:0
weight:5
aid:6
body:
2蝶华正品精致印花蕾丝花边女式聚拢型文胸套装

price:102
trueprice:36
brand:美人儿
units:
templet:
userip:127.0.0.1
redirecturl:
iid:11110
记录:6id:7
typeid:6
typeid2:0
sortrank:1370575625
flag:p
ismake:1
channel:6
arcrank:0
click:157
money:0
title:1【高校直达】黑爵A5入门级游戏键盘 正品超值键盘 有线键盘 电脑
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130607/1-13060G12I30-L.jpg
pubdate:1370575625
senddate:1370575694
mid:1
keywords:键盘,正品,超值,有线,电脑,游戏,级,直达,黑爵,A5,入
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:测试用的哦
filename:
dutyadmin:1
tackid:0
mtype:0
weight:6
aid:6
body:
2蝶华正品精致印花蕾丝花边女式聚拢型文胸套装

price:102
trueprice:36
brand:美人儿
units:
templet:
userip:127.0.0.1
redirecturl:
iid:11110
记录:7id:7
typeid:6
typeid2:0
sortrank:1370575625
flag:p
ismake:1
channel:6
arcrank:0
click:157
money:0
title:1【高校直达】黑爵A5入门级游戏键盘 正品超值键盘 有线键盘 电脑
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130607/1-13060G12I30-L.jpg
pubdate:1370575625
senddate:1370575694
mid:1
keywords:键盘,正品,超值,有线,电脑,游戏,级,直达,黑爵,A5,入
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:测试用的哦
filename:
dutyadmin:1
tackid:0
mtype:0
weight:6
aid:4
body:
3情趣内衣套装制服 女式性感透明诱惑薄

price:99
trueprice:44
brand:黑爵
units:
templet:
userip:127.0.0.1
redirecturl:
iid:11
记录:8id:4
typeid:6
typeid2:0
sortrank:1370428324
flag:p,a
ismake:1
channel:6
arcrank:0
click:106
money:0
title:3新款 情趣内衣套装制服 女式性感透明诱惑薄纱 齐B紧身睡裙8753
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130605/1-1306051S2150-L.jpg
pubdate:1370428324
senddate:1370428366
mid:1
keywords:齐,薄纱,B,紧身,8753,睡裙,诱惑,透明,套装,情趣内
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:情趣内衣套装制服 女式性感透明诱惑薄
filename:
dutyadmin:1
tackid:0
mtype:0
weight:3
aid:6
body:
2蝶华正品精致印花蕾丝花边女式聚拢型文胸套装

price:102
trueprice:36
brand:美人儿
units:
templet:
userip:127.0.0.1
redirecturl:
iid:11110
记录:9id:7
typeid:6
typeid2:0
sortrank:1370575625
flag:p
ismake:1
channel:6
arcrank:0
click:157
money:0
title:1【高校直达】黑爵A5入门级游戏键盘 正品超值键盘 有线键盘 电脑
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130607/1-13060G12I30-L.jpg
pubdate:1370575625
senddate:1370575694
mid:1
keywords:键盘,正品,超值,有线,电脑,游戏,级,直达,黑爵,A5,入
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:测试用的哦
filename:
dutyadmin:1
tackid:0
mtype:0
weight:6
aid:7
body:
测试用的哦1

price:2022
trueprice:1002
brand:美人儿
units:
templet:
userip:127.0.0.1
redirecturl:
iid:24418612187

后来改认真看了下,改了下
select * from #@__archives left join dede_addonshop on #@__archives.id=dede_addonshop.aid where #@__archives.channel=6 ORDER BY RAND() limit 12

结果就正确了
运行SQL:select * from #@__archives left join dede_addonshop on
#@__archives.id=dede_addonshop.aid where #@__archives.channel=6 ORDER BY
RAND() limit 12,共有3条记录,最大返回100条!记录:1id:4
typeid:6
typeid2:0
sortrank:1370428324
flag:p,a
ismake:1
channel:6
arcrank:0
click:106
money:0
title:3新款 情趣内衣套装制服 女式性感透明诱惑薄纱 齐B紧身睡裙8753
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130605/1-1306051S2150-L.jpg
pubdate:1370428324
senddate:1370428366
mid:1
keywords:齐,薄纱,B,紧身,8753,睡裙,诱惑,透明,套装,情趣内
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:情趣内衣套装制服 女式性感透明诱惑薄
filename:
dutyadmin:1
tackid:0
mtype:0
weight:3
aid:4
body:
3情趣内衣套装制服 女式性感透明诱惑薄

price:99
trueprice:44
brand:黑爵
units:
templet:
userip:127.0.0.1
redirecturl:
iid:11
记录:2id:7
typeid:6
typeid2:0
sortrank:1370575625
flag:p
ismake:1
channel:6
arcrank:0
click:157
money:0
title:1【高校直达】黑爵A5入门级游戏键盘 正品超值键盘 有线键盘 电脑
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130607/1-13060G12I30-L.jpg
pubdate:1370575625
senddate:1370575694
mid:1
keywords:键盘,正品,超值,有线,电脑,游戏,级,直达,黑爵,A5,入
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:测试用的哦
filename:
dutyadmin:1
tackid:0
mtype:0
weight:6
aid:7
body:
测试用的哦1

price:2022
trueprice:1002
brand:美人儿
units:
templet:
userip:127.0.0.1
redirecturl:
iid:24418612187
记录:3id:6
typeid:6
typeid2:0
sortrank:1370575548
flag:p
ismake:1
channel:6
arcrank:0
click:175
money:0
title:2蝶华正品精致印花蕾丝花边女式聚拢型文胸套装
shorttitle:
color:
writer:
source:未知
litpic:/uploads/allimg/130607/1-13060G126070-L.jpg
pubdate:1370575548
senddate:1370575620
mid:1
keywords:女式,聚拢型,文胸,套装,花边,蕾丝,正品,精致,印花,蝶华
lastpost:0
scores:0
goodpost:0
badpost:0
notpost:0
description:蝶华正品精致印花蕾丝花边女式聚拢型文胸套装
filename:
dutyadmin:1
tackid:0
mtype:0
weight:5
aid:6
body:
2蝶华正品精致印花蕾丝花边女式聚拢型文胸套装

price:102
trueprice:36
brand:美人儿
units:
templet:
userip:127.0.0.1
redirecturl:
iid:11110

希望对你有用。
参考技术A 就你贴这个B代码,谁看得明白? 参考技术B 你的数据是从3个表中取出来的? 参考技术C 是不是这三条数据有某个字段值不一样没发现啊,应该不会出现这种bug吧

以上是关于mysql联表查询出现重复数据怎么解决的主要内容,如果未能解决你的问题,请参考以下文章

mysql带条件查询,联表查询

mysql删除重复数据,保留一条

mysql连表查询出现重复的数据怎么办

mysql 如何去除表连接查询出来的重复数据

mysql多表查询出现重复数据

mysql UNION 有重复的数据,怎么解决