Mysql rownum 实现
SELECT @rownum:=@rownum+1 AS rownum, frutas.*
FROM (SELECT @rownum:=0) r, frutas;
用rownum修改自身表字段
比如:下面是对同一张表(即a=b)的sort_no进行修改,将sort_no设置为rownum:
update a inner join
(
SELECT b.id, @rownum:=@rownum+1 AS rownum
FROM (SELECT @rownum:=0) r, b where b.status!='DELETED' order by b.update_time desc
) c
on a.id=c.id
set a.sort_no=c.rownum
where a.status!='DELETED'
详细请参考:
MySQL update不能直接使用select的结果 http://www.linuxidc.com/Linux/2011-03/33845.htm
如果不像上面那么特殊,也可以写成下面这样:
(也就是把要查询的东西先放到临时表里,起个别名)
update apples
set price = (
select price from (
select * from apples
) as x
where variety = 'gala')
where variety = 'fuji';
详细请参考: