MySQL,如何通过rand()选择并更新同一查询中的列?

时间:2014-11-13 20:42:42

标签: mysql select sql-update

我需要一种通过rand()选择行的方法,并在MYSQL上的同一查询中更新它 我试过这段代码但没有回答:

SET AUTOCOMMIT=0;
BEGIN TRANSACTION;
select * from articles where status = 3 and name IS NOT NULL ORDER BY RAND() limit 0,1;
update articles set status = 4 ;
COMMIT TRANSACTION;


1 个答案:

答案 0 :(得分:0)

这为我工作:

update articles set `status` = 4 where id in (
  SELECT id FROM  (
    select id from articles where status = 3 and name IS NOT NULL ORDER BY RAND() limit 0,1
  ) tmp
);