如何更新mysql中所有重复记录的列?

时间:2014-06-08 07:28:08

标签: mysql sql

例如,我有一个名为“Qbank”的mysql表,其中包含以下列: ID(int-AUTO_INCREMENT),Question(longtext),Repeated(int,default 0)

我知道如何获得重复行数(问题) -

SELECT Question, Repeated, count(ID) as cnt FROM Qbank
GROUP BY Question HAVING cnt > 1

此外,还要获取所有重复行的列表 -

SELECT ID, Qbank.Question, Repeated FROM Qbank 
INNER JOIN (SELECT Question FROM Qbank 
GROUP BY Question HAVING count(ID) > 1) dup ON Qbank.Question = dup.Question 
Order By Question

但我想做的是 - 将所有重复项(具有相同问题)的“重复”值设置为1。(以便将这些值与剩余的非重复问题区分开来,默认情况下重复值为0)

此类查询的结构是什么?

很抱歉,对于详细说明,这是证明的唯一方法,我在询问之前已经搜索了很多,但还没有找到任何东西。

感谢。

此致

博士。阿图尔

3 个答案:

答案 0 :(得分:2)

尝试在JOIN中使用UPDATE

UPDATE Qbank T1 JOIN 
  (SELECT Question FROM Qbank 
   GROUP BY Question HAVING count(ID) > 1) dup ON T1.Question = dup.Question 
SET T1.Repeated = 1

答案 1 :(得分:0)

使用 GROUP BY 更新重复项:

START TRANSACTION;
UPDATE wlvalidation wvl
INNER JOIN 
(SELECT w.`id`,w.`active`,w.`isused` ,w.wl_key,COUNT(w.`wl_key`) AS ked FROM   wlvalidation w WHERE w.`isused`=0 AND w.`wl_company`='afr'
GROUP BY w.`wl_key`
HAVING COUNT(w.`wl_key`) > 1)drs
ON wvl.`id`=drs.`id`
SET wvl.`active`=0;

答案 2 :(得分:0)

通过取消唱sub-query将所有重复问题设置为 1:

UPDATE Qbank 
    SET Repeated = 1
WHERE Question IN (SELECT Question FROM Qbank 
   GROUP BY Question HAVING count(ID) > 1)