创建表:
复制代码 代码示例:
CREATE TABLE
mysql_testing(db_names VARCHAR(100));
INSERT INTO mysql_testing
SELECT 'SQL Server' UNION ALL
SELECT 'MySQL' UNION ALL
SELECT 'Oracle' UNION ALL
SELECT 'MongoDB' UNION ALL
SELECT 'PostGreSQL';
现在可以在两个方法中指定row_number变量。>> row_number()用法_row_number分页查询语句
方法1,设置变量row_number,并在select查询语句中使用。
复制代码 代码示例:
SET @row_number:=0;
SELECT @row_number:=@row_number+1 AS row_number,db_names FROM mysql_testing
ORDER BY db_names;
方法2,在表中使用变量,并join连接原表,实现输出行号。
复制代码 代码示例:
SELECT @row_number:=@row_number+1 AS row_number,db_names FROM mysql_testing, (SELECT @row_number:=0) AS t
ORDER BY db_names;
以上两种方法,返回结果相同,如下:
row_number db_names
1 MongoDB
2 MySQL
3 Oracle
4 PostGreSQL
5 SQL Server