繁体   English   中英

难以使用MySQL的WHERE IN子句

[英]Having difficulty with the WHERE IN clause for MySQL

好的,我意识到这可能非常简单,但是现在我的大脑已经冻结了。 这个查询需要一点帮助。 让我们分解一下。 我有两个表(每个示例),我想更新一个表的“无法交付”状态

客户表 (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);

有什么建议么? 谢谢!

UPDATE tbl_address
SET (undeliverable = 1)
WHERE customerID IN (
   SELECT customerID
   FROM tbl_customers
   WHERE custAcctNum 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM