mysql update group_MYSQL-如何为group by select语句产生的所有项目更新字段

bd96500e110b49cbb3cd949968f18be7.png

I have a select like this:

SELECT field1, field2, field3

FROM table WHERE field1= 5 AND field_flag =1

GROUP BY field1, field2, field3 limit 1000;

I want to update field_flag for the resulting rows. How can I do that in MySQL?

解决方案

Do you mean that you want to update table where field1, field2, and field3 are in the set returned by your select statement ?

eg.

update table,

( select field1, field2, field3

FROM table WHERE field1= 5 AND field_flag =1

GROUP BY field1, field2, field3 limit 1000 ) temp

set table.field_flag = 99

where table.field1=temp.field1 and table.field2=temp.field2 and table.field3 = temp.field3

Note that the update might update many more than 1000 rows.

A temporary table could be used too:

create temporary table temptab as

select field1, field2, field3

FROM table WHERE field1= 5 AND field_flag =1

GROUP BY field1, field2, field3 limit 1000

update table,

temptab temp

set table.field_flag = 99

where table.field1=temp.field1 and table.field2=temp.field2 and table.field3 = temp.field3

This has the advantage that temptab can be used later, and also that indexes can be added to speed up the update:

create index on temptab (field1, field2, field3);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值