在MySQL查询中返回结果后生成记录的行数(序列号)?
要生成序列号,即MySQL查询中的行数,请使用以下语法。
SELECT @yourVariableName − = @yourVariableName+1 anyAliasName, yourColumnName1,yourColumnName2,yourColumnName3,....N from yourTableName , (select @yourVariableName − = 0) as yourVariableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table tblStudentInformation -> ( -> StudentName varchar(20), -> StudentAge int, -> StudentMathMarks int -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into tblStudentInformation values('Carol',23,89);
mysql> insert into tblStudentInformation values('Bob',25,92);
mysql> insert into tblStudentInformation values('John',21,82);
mysql> insert into tblStudentInformation values('David',26,98);使用select语句显示表中的所有记录。查询如下-
mysql> select *from tblStudentInformation;
以下是输出。
+-------------+------------+------------------+ | StudentName | StudentAge | StudentMathMarks | +-------------+------------+------------------+ | Carol | 23 | 89 | | Bob | 25 | 92 | | John | 21 | 82 | | David | 26 | 98 | +-------------+------------+------------------+ 4 rows in set (0.00 sec)
以下是在MySQL查询中生成序列号的查询-
mysql> SELECT @serialNumber − = @serialNumber+1 yourSerialNumber, -> StudentName,StudentAge,StudentMathMarks from tblStudentInformation, -> (select @serialNumber − = 0) as serialNumber;
这是输出,以序列号的形式显示行号。
+------------------+-------------+------------+------------------+ | yourSerialNumber | StudentName | StudentAge | StudentMathMarks | +------------------+-------------+------------+------------------+ | 1 | Carol | 23 | 89 | | 2 | Bob | 25 | 92 | | 3 | John | 21 | 82 | | 4 | David | 26 | 98 | +------------------+-------------+------------+------------------+ 4 rows in set (0.00 sec)
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短