MYSQL delete all results having count(*)=1(MYSQL删除所有count(*)=1的结果)
问题描述
我有一个标记有两个字段 sesskey (varchar32, index) 和 products (int11) 的表,现在我必须删除所有按 sesskey count(*) = 1 分组的行.我尝试了几种方法,但都失败了.
I have a table taged with two fields sesskey (varchar32 , index) and products (int11), now I have to delete all rows that having group by sesskey count(*) = 1. I'm trying a fews methods but all fails.
示例:
delete from taged where sesskey in (select sesskey from taged group by sesskey having count(*) = 1)
sesskey 字段不能是主键,因为它重复了.
The sesskey field could not be a primary key because its repeated.
推荐答案
DELETE si
FROM t_session si
JOIN (
SELECT sesskey
FROM t_session so
GROUP BY
sesskey
HAVING COUNT(*) = 1
) q
ON q.sesskey = si.sesskey
您需要在此处加入.使用相关子查询将不起作用.
You need to have a join here. Using a correlated subquery won't work.
有关详细信息,请参阅我博客中的这篇文章:
See this article in my blog for more detail:
- 保持行
这篇关于MYSQL删除所有count(*)=1的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MYSQL删除所有count(*)=1的结果
基础教程推荐
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01