数据库
首页 > 数据库> > 改善MySQL中的更新

改善MySQL中的更新

作者:互联网

有时我需要生成许多插入查询,如下所示:

update claim set register_dt = '2014-01-29 09:48' where folio = '0021'
update claim set register_dt = '2014-04-09 11:30' where folio = '0080'
update claim set register_dt = '2014-05-06 13:00' where folio = '0123'
update claim set register_dt = '2014-05-21 12:20' where folio = '0161'
update claim set register_dt = '2014-01-03 10:40' where folio = '0001'
update claim set register_dt = '2014-01-06 09:12' where folio = '0002'
update claim set register_dt = '2014-01-06 10:40' where folio = '0003'
update claim set register_dt = '2014-01-07 13:00' where folio = '0004'
update claim set register_dt = '2014-01-08 11:50' where folio = '0005'
update claim set register_dt = '2014-01-10 12:34' where folio = '0006'
update claim set register_dt = '2014-01-13 09:45' where folio = '0007'
update claim set register_dt = '2014-01-15 09:29' where folio = '0008'
update claim set register_dt = '2014-01-15 10:05' where folio = '0009'
[... more and more]

我得到了这段文字,并在php或visual studio中的一些小脚本中逐个执行.

我现在想如果可以在一些唯一的查询中执行此操作,以在mysql浏览器客户端中进行全部更新. (并且不必每次都执行查询执行程序)

解决方法:

我会选择使用@GordonLinoff显示的临时表.但这从技术上讲并不是一个查询,因为您必须创建并填充临时表.

在单个查询中真正做到这一点的一种方法是使用巨大的CASE表达式:

update claim set register_dt = case folio
when '0021' then '2014-01-29 09:48'
when '0080' then '2014-04-09 11:30'
when '0123' then '2014-05-06 13:00' 
when '0161' then '2014-05-21 12:20'
when '0001' then '2014-01-03 10:40'
when '0002' then '2014-01-06 09:12'
when '0003' then '2014-01-06 10:40'
when '0004' then '2014-01-07 13:00'
when '0005' then '2014-01-08 11:50'
when '0006' then '2014-01-10 12:34'
when '0007' then '2014-01-13 09:45'
when '0008' then '2014-01-15 09:29' 
when '0009' then '2014-01-15 10:05'
. . . 
end;

您可以制作一条SQL语句,只要max_allowed_pa​​cket.

标签:sql-update,sql,mysql
来源: https://codeday.me/bug/20191029/1957923.html