我有一个varchar列的表,我想找到在这个列中有重复值的所有记录。我可以使用什么查询来查找重复项?
当前回答
SELECT *
FROM mytable mto
WHERE EXISTS
(
SELECT 1
FROM mytable mti
WHERE mti.varchar_column = mto.varchar_column
LIMIT 1, 1
)
ORDER BY varchar_column
这个查询返回完整的记录,而不仅仅是不同的varchar_column。
这个查询不使用COUNT(*)。如果有很多重复项,COUNT(*)是昂贵的,并且不需要整个COUNT(*),只需要知道是否有两行具有相同的值。
这是通过相关查询底部的LIMIT 1,1来实现的(本质上意味着“返回第二行”)。EXISTS只在前面提到的第二行存在时才返回true(即至少有两行具有相同的varchar_column值)。
当然,在varchar_column上建立索引将大大加快此查询的速度。
其他回答
Select column_name, column_name1,column_name2, count(1) as temp from table_name group by column_name having temp > 1
对GROUP BY子句执行SELECT操作。假设name是你想要在其中找到重复项的列:
SELECT name, COUNT(*) c FROM table GROUP BY name HAVING c > 1;
这将返回一个在第一列中包含名称值的结果,以及该值在第二列中出现次数的计数。
SELECT *
FROM `dps`
WHERE pid IN (SELECT pid FROM `dps` GROUP BY pid HAVING COUNT(pid)>1)
一个非常晚的贡献…万一这能帮助到以后的任何人…我有一个任务是在一个银行应用程序中找到匹配的交易对(实际上是账户到账户转账的双方),以识别每个账户间转账交易的“从”和“到”,所以我们最终得到了这个:
SELECT
LEAST(primaryid, secondaryid) AS transactionid1,
GREATEST(primaryid, secondaryid) AS transactionid2
FROM (
SELECT table1.transactionid AS primaryid,
table2.transactionid AS secondaryid
FROM financial_transactions table1
INNER JOIN financial_transactions table2
ON table1.accountid = table2.accountid
AND table1.transactionid <> table2.transactionid
AND table1.transactiondate = table2.transactiondate
AND table1.sourceref = table2.destinationref
AND table1.amount = (0 - table2.amount)
) AS DuplicateResultsTable
GROUP BY transactionid1
ORDER BY transactionid1;
The result is that the DuplicateResultsTable provides rows containing matching (i.e. duplicate) transactions, but it also provides the same transaction id's in reverse the second time it matches the same pair, so the outer SELECT is there to group by the first transaction ID, which is done by using LEAST and GREATEST to make sure the two transactionid's are always in the same order in the results, which makes it safe to GROUP by the first one, thus eliminating all the duplicate matches. Ran through nearly a million records and identified 12,000+ matches in just under 2 seconds. Of course the transactionid is the primary index, which really helped.
SELECT varchar_col
FROM table
GROUP BY varchar_col
HAVING COUNT(*) > 1;
推荐文章
- MySQL对重复键更新在一个查询中插入多行
- mysql_connect():[2002]没有这样的文件或目录(试图通过unix:///tmp/mysql.sock连接)在
- MySQL:如何复制行,但改变几个字段?
- 不能删除或更新父行:外键约束失败
- Mysql错误1452:不能添加或更新子行:外键约束失败
- MySQL DISTINCT在GROUP_CONCAT()上
- 第一次设计数据库:我是否过度设计了?
- MySQL选择一个列DISTINCT,与其他列相对应
- 错误1022 -不能写;表中重复的键
- 如何修改列和更改默认值?
- 如何在MySQL 8.0中授予root用户所有权限
- mysqld_safe UNIX套接字文件目录“/var/run/mysqld”不存在
- 配置系统初始化失败
- 快速简单的方法迁移SQLite3到MySQL?
- 用MySQL计算中位数的简单方法