2016-09-30 53 views
0

内我有2列在我的表OfficeCust_idcustomer需要uppate SQL在SQL表

的数据是像下面,我想更新cust_id谁拥有空值的客户,因为我得到了后期cust_id在表中,

EX:我需要一个更新脚本搜索谁拥有空cust_id客户和搜索cust_id和更新 这里DDD客户(cust_id = 4),所以我需要一个脚本来更新

cust_id, Customer 
1 AAA 
2 BBB 
3 CCC 
null DDD 
null EEE 
4 DDD 
5 CCC 
7 EEE 
+0

你的表格可以复制'cust_id'? – Barmar

+0

是的,它允许dup cust_id! – Manila

+0

为什么你会为同一个客户有两行?这看起来像是一个数据库正常化问题。 – Barmar

回答

0

与使用UPDATE自联接:

UPDATE Office AS o1 
JOIN Office AS o2 ON o1.Customer = o2.Customer 
SET o1.cust_id = o2.cust_id 
WHERE o1.cust_id IS NULL 
AND o2.cust_id IS NOT NULL 
+0

感谢Barmar为快速周转我现在会尝试这个 – Manila

+0

真棒这个工作谢谢! – Manila

+0

非常感谢Barmar! – Manila