2016-09-19 61 views
0

如何在更新語句中插入查詢?在語句中插入查詢

的如: -

UPDATE TABLE1 
SET 
COLUMN1 = 'select column1 as "FRUIT", column2 as "PRICE" from BASKET where column1='APPLE'' 
+0

你只是想更新蘋果的價格?或者你想把蘋果變成另一種水果? –

+0

您想在第1列中插入語句本身還是在第1列的值中插入? – Jens

+2

您使用的是MySQL還是DB2? (不要標記不涉及的產品。) – jarlh

回答

0

試試這個方法:

UPDATE TABLE1 
SET COLUMN1 = (SELECT column1 FROM BASKET where column1='XX' LIMIT 1), 
COLUMN2 = (SELECT column2 FROM BASKET where column2='YY' LIMIT 1) 
WHERE column1='APPLE' 
0

在DB2 for更新與其他表的表,你可以做到這一點

UPDATE TABLE1 t1 
    SET (t1.COLUMN1, t1.COLUMN2) = (
    select t2.COLUMN1, t1.COLUMN2 from TABLE2 t2 
    where t1.key1=t2.key1 
) 
    where exists 
    (
    select * from TABLE2 t2 
    where t1.key1=t2.key1 
)