oracle查询行内号可以用 row_number() voer(partition by xxx order by xxx)
后来改用mysql发现没有这个函数,从网上查询后发现可以这样写:
-- 行内号 group by的暂存值
SET @row_number=0, @customer_no=0;
SELECT
-- 如果与上一个相同 行内号+1
@row_number:=CASE
WHEN @customer_no = s.age THEN @row_number + 1
ELSE 1 --否则从1开始
END AS num,
@customer_no:=s.age AS stu_age, --对group by 的内容赋值
s.id,
s.name
FROM
student s
ORDER BY
s.age; -- 注意要用group by的内容去排序 才能保证行内号的取值