mysql 如何在UPDATE查询中执行三个表JOIN?

gcuhipw9  于 2023-04-28  发布在  Mysql
关注(0)|答案(7)|浏览(167)

我问了一个问题,得到了这个帮助的回答。

UPDATE TABLE_A a JOIN TABLE_B b
   ON a.join_col = b.join_col AND a.column_a = b.column_b
   SET a.column_c = a.column_c + 1

现在我希望这样做,如果有三个表涉及这样的东西。

UPDATE tableC c JOIN tableB b JOIN tableA a

我的问题基本上是。..是否可以在UPDATE语句上执行三个表连接?它的正确语法是什么?
我应该做以下操作吗?

JOIN tableB, tableA
 JOIN tableB JOIN tableA
n8ghc7c1

n8ghc7c11#

答案是“是的”,你可以。
试试这样:

UPDATE TABLE_A a
    JOIN TABLE_B b ON a.join_col = b.join_col AND a.column_a = b.column_b
    JOIN TABLE_C c ON [condition]
SET a.column_c = a.column_c + 1

一般 update join:

UPDATE TABLEA a
JOIN TABLEB b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
vwoqyblh

vwoqyblh2#

实现相同结果的另一种方法是根本不使用JOIN关键字。

UPDATE TABLE_A, TABLE_B
SET TABLE_A.column_c = TABLE_B.column_c + 1
WHERE TABLE_A.join_col = TABLE_B.join_col
zzzyeukh

zzzyeukh3#

下面是包含JOINWHEREupdate 查询。同样,我们可以使用多个join/where子句:

UPDATE opportunities_cstm oc JOIN opportunities o ON oc.id_c = o.id
 SET oc.forecast_stage_c = 'APX'
 WHERE o.deleted = 0
   AND o.sales_stage IN('ABC','PQR','XYZ')
esbemjvw

esbemjvw4#

另一个 * 总计划 *:

UPDATE table A
JOIN table B ON {join fields}
JOIN table C ON {join fields}
JOIN {as many tables as you need}
SET A.column = {expression}

示例:

UPDATE person P
JOIN address A ON P.home_address_id = A.id
JOIN city C ON A.city_id = C.id
SET P.home_zip = C.zipcode;
4dbbbstv

4dbbbstv5#

是的,您可以为update语句执行三表联接。下面是一个例子:

UPDATE customer_table c

  JOIN
      employee_table e
      ON c.city_id = e.city_id
  JOIN
      anyother_table a
      ON a.someID = e.someID

SET c.active = "Yes"
WHERE c.city = "New york";
qvtsj1bj

qvtsj1bj6#

对于PostgreSQL示例:

UPDATE TableA AS a
SET param_from_table_a=FALSE -- param FROM TableA
FROM TableB AS b
WHERE b.id=a.param_id AND a.amount <> 0;
yquaqz18

yquaqz187#

没有一个答案对我不起作用我在mysql手册上找到了这个

UPDATE T1,T2 INNER JOIN T2 ON T1.C1 = T2.C1 SET T1.C2 = T2.C2,       T2.C3 = expr WHERE condition

相关问题