gpt4 book ai didi

c# - SQL:使用带有 LIMIT 的 INNER JOIN 进行更新

转载 作者:行者123 更新时间:2023-11-29 04:48:20 26 4
gpt4 key购买 nike

我正在尝试更新数据库中的特定列。

此查询有效:

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
);

如何优化此查询再次感谢!

最佳答案

你就是做不到。

根据 MySQL docs for UPDATE :

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

关于c# - SQL:使用带有 LIMIT 的 INNER JOIN 进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15128189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com