我正在嘗試更新數據庫中的特定列。SQL:UPDATE INNER JOIN WITH LIMIT
此查詢的工作:
UPDATE table1 A INNER JOIN table2 B
ON A.type = B.typeName
SET A.closed = 0, A.sample = 0
WHERE A.`status` IN ('Finished', 'Exception', 'Query') AND A.date BETWEEN '2013-01-01' AND '2013-01-31'
AND A.code IN ('ex1','ex2','ex3')
AND A.closed = 0 AND B.order = 'Non-Order' AND A.userName = 'test';
但是,當我試圖把一個極限,它說:
Incorrect usage of UPDATE and LIMIT
UPDATE table1 A INNER JOIN table2 B
ON A.type = B.typeName
SET A.closed = 0, A.sample = 0
WHERE A.`status` IN ('Finished', 'Exception', 'Query') AND A.date BETWEEN '2013-01-01' AND '2013-01-31'
AND A.code IN ('ex1','ex2','ex3')
AND A.closed = 0 AND B.order = 'Non-Order' AND A.userName = 'test' LIMIT 3;
我怎樣才能使這個更新與限制?非常感謝!
[EDIT]
我已經做了我想要的,但它很慢,花了6秒更新3行。
這裏的查詢:
UPDATE table1 SET closed=1, sample=1
WHERE id IN (
SELECT id FROM (
SELECT id FROM table1 A
INNER JOIN table2 B ON A.type = B.typeName
WHERE A.`status` IN ('Finished', 'Exception', 'Query') AND A.date BETWEEN '2013-01-01' AND '2013-01-31'
AND A.code IN ('ex1','ex2','ex3')
AND A.closed = 0 AND B.order = 'Non-Order' AND A.userName = 'test' LIMIT 3
) tmp
);
我怎麼能再次優化這個查詢的感謝!
給他一個解決方法。做到這一點。 – Scotch 2013-02-28 05:43:20
@JW感謝您的鏈接,請參閱我的編輯!謝謝! – jomsk1e 2013-02-28 06:23:54
如何更新asnwer。 – 2013-03-01 00:29:09