MySQL的UNION更新 [英] MySql UNION for UPDATE

查看:115
本文介绍了MySQL的UNION更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以使用单个SQL查询为每行更新具有不同值的多行?我必须用不同的数据更新许多行中的一个列.对每一行使用单独的更新查询似乎是多余的,因此,如果可能的话,我想将此过程合并为一个SQL语句,或者至少减少所需的查询数量.

Is there a way to update multiple rows with different values for each row using a single SQL query? I have to update one colum in many rows with different data. Using individual update queries for each row seems excessive so if it's possible I would like to consolidate this process into a single SQL statement or at least reduce the number of queries required.

我在Zend框架和MySql中使用PHP.

I am using PHP with the Zend framework and MySql.

推荐答案

创建一个临时表并将其填充为:

Create a temporary table and fill it with:

CREATE TEMPORARY TABLE temptable (id INTEGER, VALUE VARCHAR(200))

INSERT
INTO temptable
VALUES
  ('1', 'val1'),
  ('2', 'val2'),
  ('3', 'val3'),
  ('4', 'val4')

然后发布:

UPDATE
  mytable m, temptable t
SET m.value = t.value
WHERE m.id = t.id

这篇关于MySQL的UNION更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆