gpt4 book ai didi

mysql - 更新子查询的 WHERE 子句中的 UNION

转载 作者:行者123 更新时间:2023-11-29 11:41:25 25 4
gpt4 key购买 nike

是否有任何理由不应该在更新子查询的 WHERE 子句中使用 UNION?或者就此而言,即使是普通的选择子查询?

这样的查询有没有更好的方法来消除 UNION?

请注意,对于我的情况,UNION 将产生相当少量的记录。

UPDATE mytable
set mytable.bla='xxx'
WHERE id IN (
SELECT id
FROM t1
INNER JOIN t2 ON t2.t1_id=t1.id
LEFT OUTER JOIN t3 ON t3.t1_id=t2.id
WHERE t2.id IN (1,2,3) AND t3.id IS NULL
UNION
SELECT id FROM t4
INNER JOIN t5 ON t5.id=t4.t5_id
LEFT OUTER JOIN t6 ON t6.t5_id=t5.id
WHERE t5.parent_id IN (1,2,3) AND t6.id IS NULL
);

最佳答案

将其切换为联接:-

UPDATE mytable 
INNER JOIN
(
SELECT id
FROM t1
INNER JOIN t2 ON t2.t1_id=t1.id
LEFT OUTER JOIN t3 ON t3.t1_id=t2.id
WHERE t2.id IN (1,2,3)
AND t3.id IS NULL
UNION
SELECT id
FROM t4
INNER JOIN t5 ON t5.id=t4.t5_id
LEFT OUTER JOIN t6 ON t6.t5_id=t5.id
WHERE t5.parent_id IN (1,2,3)
AND t6.id IS NULL
) sub0
ON mytable.id = sub0.id
SET mytable.bla='xxx'

关于mysql - 更新子查询的 WHERE 子句中的 UNION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35724993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com