我怎麼能寫只是一個SQL查詢來獲取相同的結果通過執行這兩個查詢(步驟):嵌套SQL查詢
第一步:
SELECT old_id, new_id FROM history WHERE flag = 1;
結果:
+--------+--------+
| old_id | new_id |
+--------+--------+
| 11 | 22 |
| 33 | 44 |
| 55 | 66 |
+--------+--------+
,然後使用以前的結果,執行這個查詢:
UPDATE other_tabla SET somefk_id = CASE somefk_id
WHEN 11 THEN 22
WHEN 33 THEN 44
WHEN 55 THEN 66
END WHERE somefk_id IN (11,33,55)
+1`join`比`table1,table2`更具可讀性:) – Andomar 2011-01-26 16:31:04