MySQL多表联合查询过滤重复数据怎么写
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL多表联合查询过滤重复数据怎么写相关的知识,希望对你有一定的参考价值。
目前有三张表,分别为花名册,取证情况,复审情况。具体字段及数据如下:
花名册:
序号 工号 姓名
1 1001 张三
2 1002 李四
3 1003 王五
4 1004 赵六
取证情况:
序号 工号 姓名 证书信息
1 1001 张三 A证书
2 1002 李四 A证书
3 1003 王五 A证书
复审情况
序号 工号 姓名 复审时间
1 1001 张三 2016年3月1日
2 1001 张三 2017年3月1日
3 1002 李四 2017年5月1日
现在用关联查询语句是:select 花名册.工号,花名册.姓名,取证情况.证书信息,复审情况.复审时间 from (花名册 left join 取证情况 on 花名册.工号=取证情况.工号) left join 复审情况 on 花名册.工号=复审情况.工号;进行查询。
现在的问题是如果同一个人多次复审时查询出来的结果会出现多条数据,现在的问题时如何过滤多条数据让它只出现1条,并且按照复审情况中的最近复审时间来过滤显示。
复审表通过一次自连接筛选出最大值后再做关联
花名册.工号,
花名册.姓名,
取证情况.证书信息,
复审情况F.复审时间
FROM
花名册
LEFT JOIN 取证情况 ON 花名册.工号 = 取证情况.工号
LEFT JOIN (
SELECT 复审情况.* FROM 复审情况
INNER JOIN
(SELECT MAX(复审时间) 复审时间,工号 FROM 复审情况 GROUP BY 工号) 复审情况2
ON 复审情况.复审时间=复审情况2.复审时间 AND 复审情况.工号=复审情况2.工号
) 复审情况F ON 花名册.工号 = 复审情况F.工号; 参考技术A mysql多表联合查询过滤重复数据怎么写
SELECT COUNT(DISTINCT <a href="https://www.baidu.com/s?wd=AA&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3rjNWuWbzm19Wm1DzrADY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnH61nH0knW0Yn1RYn1cdPj6zn0" target="_blank" class="baidu-highlight">AA</a>.字段)
FROM (SELECT 字段
FROM TABLE1
UNION ALL
SELECT 字段 FROM TABLE2) <a href="https://www.baidu.com/s?wd=AA&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3rjNWuWbzm19Wm1DzrADY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnH61nH0knW0Yn1RYn1cdPj6zn0" target="_blank" class="baidu-highlight">AA</a>;
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多表联合查询过滤重复数据怎么写的主要内容,如果未能解决你的问题,请参考以下文章