gpt4 book ai didi

mysql - MySQL 的 WHERE IN 子句有困难

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:52 25 4
gpt4 key购买 nike

好吧,我意识到这可能非常简单,但我的大脑现在卡住了。此查询需要一些帮助。让我们分解一下。我有两个表(按照这个例子),我想更新一个表的“无法交付”状态

客户表 (tbl_customers):

+------------+-------------+
| customerID | custAcctNum |
+------------+-------------+
| 1 | 100100121 |
| 2 | 100100122 |
| 3 | 100100123 |
| 4 | 100100124 |
| 5 | 100100125 |
+------------+-------------+

地址表(tbl_address):

+-----------+------------+---------------+
| addressID | customerID | undeliverable |
+-----------+------------+---------------+
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 3 | 0 |
| 4 | 4 | 0 |
| 5 | 5 | 0 |
+-----------+------------+---------------+

具有“无法交付”的客户帐号 (custAcctNum) 的数据集

100100121, 100100123, 100100124

查询会将地址表更新为此

+-----------+------------+---------------+
| addressID | customerID | undeliverable |
+-----------+------------+---------------+
| 1 | 1 | 1 |
| 2 | 2 | 0 |
| 3 | 3 | 1 |
| 4 | 4 | 1 |
| 5 | 5 | 0 |
+-----------+------------+---------------+

这是我尝试使用的查询

UPDATE tbl_address
SET undeliverable = 1 WHERE
( SELECT custAcctNum FROM tbl_customers AS c
INNER JOIN tbl_address AS a ON a.customerID = c.customerID )
IN ( 100100121, 100100123, 100100124);

有什么建议吗?谢谢!

最佳答案

使用mysql的多表更新语法:

update tbl_Address t 
join custAcctNum c
on c.customerid = t.customerid
set t.undeliverable = 1
where c.custAcctNum in (100100121, 100100123, 100100124)

关于mysql - MySQL 的 WHERE IN 子句有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17751072/

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