MySQL版本8.0中包含的Row_NUMBER()。这是一种窗口函数。这可用于为行分配序列号。要了解,请借助CREATE pcommand创建一个表-
CREATE table rowNumberDemo -> ( -> FirstName varchar(100) -> );
INSERT into rowNumberDemo values('john'); INSERT into rowNumberDemo values('john'); INSERT into rowNumberDemo values('Bob'); INSERT into rowNumberDemo values('Smith');
我们可以在SELECT语句的帮助下显示所有记录-
SELECT * from rowNumberDemo;
以下是输出-
+-----------+ | FirstName | +-----------+ | john | | john | | Bob | | Smith | +-----------+ 4 rows in set (0.00 sec)
现在,我们可以使用row_number()为每条记录分配一个递增值-
SELECT row_number() over ( order by firstName) RowNumberSqeuence,FirstName from rowNumberDemo -> order by FirstName;
以下是输出-
+-------------------+-----------+ | RowNumberSqeuence | FirstName | +-------------------+-----------+ | 1 | Bob | | 2 | john | | 3 | john | | 4 | Smith | +-------------------+-----------+ 4 rows in set (0.00 sec)
看一下输出,借助于row_number()我们为每个记录分配了一个数字。