我有一个包含3列的表格,如下所示:
列:
我想从表中检索随机条目,并将Used
列更新为1
。但是,当我运行我的代码 - 如下所示 - 它返回0
行并且不会返回任何想法。我想知道为什么会这样?
代码
UPDATE channels
SET Used = 1
WHERE ID IN (
SELECT ID
FROM (select ID
FROM channels
WHERE Used != 0
ORDER BY RAND()
LIMIT 1) x);
答案 0 :(得分:1)
解决此问题的一种方法是执行以下操作:
SET @uid := (SELECT ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);
UPDATE channels SET Used = 1 WHERE ID = @uid;
SELECT * FROM channels WHERE ID = @uid;