如果MySQL中的count大于5则更新

时间:2013-09-21 06:44:50

标签: mysql sql sql-update

我正在尝试创建一个更新table_1的查询,其中列id_owner的{​​{1}}列数超过5 rows,需要设置列“ <{1}}“至owner id这些用户拥有的所有行。

我尝试了几种不同的方法,并且每种方法都空了。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

将此UPDATE查询与JOIN一起使用即可实现此目的:

UPDATE table1 t1 
  JOIN
  (
    SELECT id_owner
      FROM table1
     GROUP BY id_owner
    HAVING COUNT(*) > 5
  ) t2
  ON t1.id_owner = t2.id_owner
  SET t1.active = 3;

请参阅this sample SQLFiddle

答案 1 :(得分:1)

update table_1
set active = 3
where owner_id in
(
  select * from
  (
    select owner_id
    from table_1
    group by owner_id
    having count(*) > 5
  ) x
)

SQLFiddle demo

答案 2 :(得分:1)

你可以试试这个: -

update table_1
set active = 3
where owner_id in
(
  select * from
  (
    select owner_id
    from table_1
    group by owner_id
    having count(*) > 5
  ) a
)