2015-05-03 51 views
0

SQL命令:哪裏是我的SQL代碼中的錯誤?

UPDATE table1 
SET colOne=1 
WHERE id EXISTS (
SELECT IF(col1=19,col2,col1) AS id 
FROM table2 
WHERE 
(col3=19 AND col4='X') 
OR 
(col3=0 AND col4='Y' AND (col1=19 OR col2=19)) 
) 

SELECT查詢單獨做工精細

+0

查詢有什麼不正確? – Alex

+0

我不知道,但它不工作,並有一個錯誤附近EXISTS – Simon

+2

嘗試用'IN'替換'EXISTS' – Alex

回答

2
UPDATE table1 
SET colOne=1 
WHERE id IN (
SELECT IF(col1=19,col2,col1) AS id 
FROM table2 
WHERE 
(col3=19 AND col4='X') 
OR 
(col3=0 AND col4='Y' AND (col1=19 OR col2=19)) 
) 
0

您的意思是使用=代替存在

UPDATE table1 
SET colOne=1 
WHERE id = (
SELECT IF(col1=19,col2,col1) AS id 
FROM table2 
WHERE 
(col3=19 AND col4='X') 
OR 
(col3=0 AND col4='Y' AND (col1=19 OR col2=19)) 
) 
+0

即使我嘗試,但不工作 – Simon

0

,你也可以做到這一點

UPDATE table1 
SET colOne=1 
WHERE EXISTS (
SELECT IF(col1=19,col2,col1) AS id 
FROM table2 
WHERE 
(col3=19 AND col4='X') 
OR 
(col3=0 AND col4='Y' AND (col1=19 OR col2=19)) 
)