如何从行中选择数据并一次更新?

时间:2017-12-14 06:27:34

标签: mysql sql database select

我为一个表做了两个查询。

SELECT url FROM links WHERE post = '0' AND keywords = 'key';

然后

UPDATE links SET post='1' WHERE keywords = 'key';

但是因为很多线程我得到了重复。 如何将这两个请求合二为一? 感谢

3 个答案:

答案 0 :(得分:2)

这称为种族条件,给以下链接一个阅读

http://www.sqlines.com/mysql/how-to/select-update-single-statement-race-condition

答案 1 :(得分:1)

试试这个

UPDATE links SET post='1' WHERE keywords = 'key' AND post = '0';

OR

UPDATE links SET post='1' WHERE url in 
(SELECT url FROM links WHERE post = '0' AND keywords = 'key');

答案 2 :(得分:1)

这可能适合你。

UPDATE links SET post='1' WHERE keywords in 
(SELECT keywords FROM links WHERE post = '0' AND keywords = 'key');