栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

MySQL使用多列为重复选择记录

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MySQL使用多列为重复选择记录

如果要计算多个列之间的重复项,请使用

group by

select ColumnA, ColumnB, ColumnC, count(*) as NumDuplicatesfrom tablegroup by ColumnA, ColumnB, ColumnC

如果只希望重复的值,则计数大于1。可使用以下

having
子句获得此值:

select ColumnA, ColumnB, ColumnC, count(*) as NumDuplicatesfrom tablegroup by ColumnA, ColumnB, ColumnChaving NumDuplicates > 1

如果您实际上希望所有重复的行都返回,则将最后一个查询返回到原始数据:

select t.*from table t join     (select ColumnA, ColumnB, ColumnC, count(*) as NumDuplicates      from table      group by ColumnA, ColumnB, ColumnC      having NumDuplicates > 1     ) tsum     on t.ColumnA = tsum.ColumnA and t.ColumnB = tsum.ColumnB and t.ColumnC = tsum.ColumnC

假设所有列值都不为NULL,这将起作用。如果是这样,请尝试:

     on (t.ColumnA = tsum.ColumnA or t.ColumnA is null and tsum.ColumnA is null) and        (t.ColumnB = tsum.ColumnB or t.ColumnB is null and tsum.ColumnB is null) and        (t.ColumnC = tsum.ColumnC or t.ColumnC is null and tsum.ColumnC is null)

编辑:

如果有

NULL
值,也可以使用
NULL
-safe运算符:

     on t.ColumnA <=> tsum.ColumnA and        t.ColumnB <=> tsum.ColumnB and        t.ColumnC <=> tsum.ColumnC


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/416759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号