我正在尝试更新数据库中的特定列。
此查询有效:
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
);
如何再次优化此查询!
答案 0 :(得分:4)
你不能。
For the multiple-table syntax, UPDATE updates rows in each table named in
table_references that satisfy the conditions. In this case, ORDER BY and LIMIT
cannot be used.
更新1
UPDATE table1 a
INNER JOIN
(
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 ON a.ID = tmp.ID
SET a.closed = 1,
a.sample = 1
答案 1 :(得分:0)
LIMIT
只能在SELECT
语句中使用。如果你想限制没有。对于UPDATE语句受影响的记录,您必须使用WHERE。