I am trying to update specific column in my database.
This query works:
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';
But when I tried to put a limit, it says:
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;
How can I make this Update with the limit? thanks a lot!
[EDIT]
I already do what I want, but it is slow, took 6 secs to update 3 rows.
Here's the query:
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
);
How can I optimize this query thanks again!