Mysql Query在结果中创建序列号,where条件适用于该序列号

时间:2015-01-16 10:14:40

标签: mysql sql select where-clause row-number

我需要一个查询,它会产生一个序列号,还有一个where条件来过滤那个序列号,例如"其中serial_number介于0到10&#34之间;

Select  @rown:=@rown+1 serial_number, user_name as un from user
                            (select @rown := 0) as t
                            where                 
                             (@rown := (@rown + 1)) between 0 and 10
                            ORDER BY u.id_user asc

但我没有得到两者之间的结果, 请帮助我

1 个答案:

答案 0 :(得分:0)

您必须使用subquery来实现此目的:

试试这个:

SELECT serial_number, un 
FROM (SELECT (@rown:=@rown+1) AS serial_number, user_name AS un 
      FROM USER, (SELECT @rown := 0) AS t 
      ORDER BY u.id_user ASC
    ) AS A
WHERE serial_number BETWEEN 0 AND 10