我有一个表并且在其中有一个特定值 - 默认情况下它设置为-1,但我想将其更改为0以获得随机行。
此操作的正确查询是什么?
以下是我的尝试:
UPDATE statuses SET status = 0
WHERE word_id = (
SELECT word_id FROM statuses WHERE status = -1 ORDER BY RANDOM() LIMIT 1
)
答案 0 :(得分:3)
嗯,我刚刚制作了一个示例表,您的查询似乎按照书面形式工作:
sqlite> create table statuses (word_id, status default -1);
sqlite> insert into statuses (word_id) values (1);
sqlite> insert into statuses (word_id) values (2);
sqlite> insert into statuses (word_id) values (3);
sqlite> insert into statuses (word_id) values (4);
sqlite> insert into statuses (word_id) values (5);
sqlite> select * from statuses;
1|-1
2|-1
3|-1
4|-1
5|-1
sqlite> UPDATE statuses SET status = 0
...> WHERE word_id = (
...> SELECT word_id FROM statuses WHERE status = -1 ORDER BY RANDOM() LIMIT 1
...> );
sqlite> select * from statuses;
1|-1
2|-1
3|0
4|-1
5|-1
因此,换句话说,您的查询是正确的 - 您的错误可能在您的代码中的其他位置。