MySQL的WHERE IN子句有困难

时间:2013-07-19 16:28:15

标签: mysql where

好的,我意识到这可能非常简单,但我的大脑现在已经冻结了。需要对此查询提供一些帮助。让我们分解吧。 我有两个表(根据此示例),我想更新单个表“无法传递”状态

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

有什么建议吗?谢谢!

2 个答案:

答案 0 :(得分:2)

UPDATE tbl_address
SET (undeliverable = 1)
WHERE customerID IN (
   SELECT customerID
   FROM tbl_customers
   WHERE custAcctNum IN (100100121, 100100123, 100100124)
);

答案 1 :(得分:2)

使用mysql的多表更新语法:

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