2012-08-09 87 views
1

我可以結合這樣Mysql加入幾個查詢在一個?

first: UPDATE table SET col1=1 WHERE id='x'; 

second: UPDATE table SET col1=0 WHERE id='y'; 

兩個查詢我可以加入一個這些查詢?

回答

4
UPDATE table 
SET col1 = CASE id WHEN 'x' THEN 1 ELSE 0 END 
WHERE id IN ('x','y') 
0

使用此SQL查詢:

UPDATE table 
SET col1 = 
CASE id 
WHEN 'x' THEN 1 
WHEN 'y' THEN 0 
END 
WHERE id IN ('x','y'); 

另見Multiple Updates in MySQL

0
UPDATE table 
SET col1 = (id = 'x') 
WHERE id IN ('x','y');