需要帮助查询重复数据的最大值,按多列分组

时间:2014-02-21 17:15:22

标签: mysql sql-update inner-join having-clause

我有大量重复的数据,我试图只更新数量最多的行,按多列分组。我试过这个,但它不起作用。任何帮助,将不胜感激。感谢

UPDATE `polls` AS `p1`
INNER JOIN (
   SELECT *
   FROM `polls`
   GROUP BY `server_id`, `product_id`, `poll_date`
   ORDER BY max(quantity)
   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';

2 个答案:

答案 0 :(得分:1)

试试这个

 ....
 INNER JOIN (
   SELECT * , max(quantity)
   FROM `polls`
   GROUP BY `server_id`, `product_id`
   ORDER BY max(quantity) desc 
 )
 ....

答案 1 :(得分:0)

use this
 INNER JOIN (
   SELECT *
   FROM 'polls'
   GROUP BY `server_id`, `product_id`, `poll_date`
   ORDER BY quantity desc limit 1
 )