用于更新的 MySql UNION

发布于 2024-07-13 13:52:25 字数 163 浏览 6 评论 0原文

有没有办法使用单个 SQL 查询更新多行,每行具有不同的值? 我必须用不同的数据更新多行中的一列。 对每一行使用单独的更新查询似乎过多,因此如果可能的话,我希望将此过程合并到单个 SQL 语句中,或者至少减少所需的查询数量。

我将 PHP 与 Zend 框架和 MySql 结合使用。

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.

I am using PHP with the Zend framework and MySql.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

厌味 2024-07-20 13:52:25

创建一个临时表并填充:

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

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')

Then issue:

UPDATE
  mytable m, temptable t
SET m.value = t.value
WHERE m.id = t.id
纸短情长 2024-07-20 13:52:25

具体不了解 MySQL,但要根据 SELECT 或多个 SELECT 的 UNION 更新多行,我会执行

UPDATE U
SET MyColumn = T.OtherColumn
FROM MyUpdateTable AS U
     JOIN
     (
         SELECT [OtherColumn] = OtherColumn1
         FROM MyOtherTable1
         WHERE ...
         UNION
         SELECT OtherColumn2
         FROM MyOtherTable2
         WHERE ...
     ) AS T
         ON T.ID = U.ID

Update 10/28/2014,转换为适用于 MySQL:

UPDATE MyUpdateTable AS U
    JOIN
    (
        SELECT [OtherColumn] = OtherColumn1
        FROM MyOtherTable1
        WHERE ...
        UNION
        SELECT OtherColumn2
        FROM MyOtherTable2
        WHERE ...
    ) AS T
        ON T.ID = U.ID
        SET MyColumn = T.OtherColumn

Don't know about MySQL specifically, but to update multiple rows based on a SELECT, or a UNION of multiple SELECTs, I would do

UPDATE U
SET MyColumn = T.OtherColumn
FROM MyUpdateTable AS U
     JOIN
     (
         SELECT [OtherColumn] = OtherColumn1
         FROM MyOtherTable1
         WHERE ...
         UNION
         SELECT OtherColumn2
         FROM MyOtherTable2
         WHERE ...
     ) AS T
         ON T.ID = U.ID

Update 10/28/2014, converted to work for MySQL:

UPDATE MyUpdateTable AS U
    JOIN
    (
        SELECT [OtherColumn] = OtherColumn1
        FROM MyOtherTable1
        WHERE ...
        UNION
        SELECT OtherColumn2
        FROM MyOtherTable2
        WHERE ...
    ) AS T
        ON T.ID = U.ID
        SET MyColumn = T.OtherColumn
蔚蓝源自深海 2024-07-20 13:52:25

我知道这适用于 SQL Server,因此值得在 MySQL 中尝试。

update xtable
set a = 
   Case
     when a = "a"
     then z
     when a = "b"
     then y
   End
where ...

您可以根据不同的行构建 case 语句。

I know this works for SQL Server, so it's worth a try in MySQL.

update xtable
set a = 
   Case
     when a = "a"
     then z
     when a = "b"
     then y
   End
where ...

You can construct the case statement based on your different rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文