我有一个基于多列的重复数据表。我想出了这个查询来查找所有重复的行
select *
from polls
group by server_id, product_id, poll_date
having count(*) > 1;
如何更新这些结果并将“updated_by”字段设置为“admin”。
我试过这样做,但它对我不起作用:(
update polls
set updated_by='admin'
group by server_id, product_id, poll_date
having count(*) > 1;
感谢您的帮助
答案 0 :(得分:0)
您应该可以将SELECT
加入UPDATE
。
UPDATE `polls` AS `p1`
INNER JOIN (
SELECT *
FROM `polls`
GROUP BY `server_id`, `product_id`, `poll_date`
HAVING COUNT(*) > 1
) AS `p2`
ON `p2`.`server_id` = `p1`.`server_id`
AND `p2`.`product_id` = `p1`.`product_id`
AND `p2`.`poll_date` = `p1`.`poll_date`
SET `p1`.`updated_by` = 'admin';
当然最好直接加入主键(如果有的话)。