本文介绍了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的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!