Home > Database > Mysql Tutorial > MySQL中如何在选出的结果集中打印某条记录的位置

MySQL中如何在选出的结果集中打印某条记录的位置

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 16:57:07
Original
1051 people have browsed it

在MySQL中,有时候我们要选出N条记录,希望在结果中每条记录中加上一列标记该记录是第几条。 这个问题在oracle中,用rownum就可以

在MySQL中,有时候我们要选出N条记录,希望在结果中每条记录中加上一列标记该记录是第几条。
这个问题在Oracle中,用rownum就可以简单的实现,,但是在MySQL中,怎么去实现呢?下面给大家举一个例子:
select t.page,t.pv,@rn:=if(1=1,@rn+1,@rn) as rn
from
(select
page,
pv 
from  
module_page_statis
where
statisDay='2010-05-31' and module='new_info' order by 2 desc  limit 10) t, (select @rn:=0) t2
order by t.pv desc


这里略微解释下:
1,(select @rn:=0) 仅仅起到初始化的作用。
2,@rn:=if(1=1,@rn+1,@rn) 这一句是每选出一条记录出来,执行一次。当然这里面还可以填充更复杂的逻辑。

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template