2016-03-04 47 views
0

如果address_1爲空,但是address_2不是,我需要更新Client表中的所有記錄。在這些情況下,我想將address_2轉換爲address_1。這裏是我的查詢到目前爲止:如何在DB2中更新多行的同一列

UPDATE Client SET Address_1 = 'address1', address_2 = '' 
WHERE client_id = 'client_id' 

但不是傳遞client_id,我想更新每個記錄。

+0

也許離開了'where'標準 - 這將更新整個表?你究竟想要做什麼? – sgeddes

+0

我試圖更新我們的表格,其中地址1是空的,但地址2不是,並且將地址2移動到地址1 – OVO

+1

'更新客戶端集合Address_1 = address_2,address_2 =''其中Address_1 =''和地址2 <>'' ' –

回答

1

您所需要的查詢是

UPDATE client SET address_1 = address_2, address_2 = '' 
WHERE address_1 = '' AND address_2 != '' 

WHERE,發現所有的問題行,然後將它移動到address_2address_1出空白address_2

注意:確保你不會混淆空字符串''NULL。在DB2中,這些不一樣。如果你的價值觀實際上是NULL,您的查詢將需要:

UPDATE client SET address_1 = address_2, address_2 = NULL 
WHERE address_1 IS NULL AND address_2 IS NOT NULL