MySQL或MariaDB查询数据添加序号

1.通用方法:使用@变量

SELECT @row_number := @row_number + 1 AS row_number, a.dept_name 
FROM sys_department a,(SELECT @row_number:=0) b ORDER BY dept_name;

2.高版本即MySQL 8.0或MariaDB10.5开始支持的ROW_NUMBER()函数

SELECT ROW_NUMBER() OVER (ORDER BY column_name) AS row_number, column_name
FROM table_name;

 

posted @ 2023-09-07 10:39  dirgo  阅读(168)  评论(0编辑  收藏  举报