
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Generate the row count (serial number) of records after returning the result in MySQL query?
To generate serial number i.e. row count in MySQL query, use the following syntax.
SELECT @yourVariableName − = @yourVariableName+1 anyAliasName, yourColumnName1,yourColumnName2,yourColumnName3,....N from yourTableName , (select @yourVariableName − = 0) as yourVariableName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table tblStudentInformation -> ( -> StudentName varchar(20), -> StudentAge int, -> StudentMathMarks int -> ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into tblStudentInformation values('Carol',23,89); Query OK, 1 row affected (0.18 sec) mysql> insert into tblStudentInformation values('Bob',25,92); Query OK, 1 row affected (0.22 sec) mysql> insert into tblStudentInformation values('John',21,82); Query OK, 1 row affected (0.15 sec) mysql> insert into tblStudentInformation values('David',26,98); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from tblStudentInformation;
The following is the output.
+-------------+------------+------------------+ | StudentName | StudentAge | StudentMathMarks | +-------------+------------+------------------+ | Carol | 23 | 89 | | Bob | 25 | 92 | | John | 21 | 82 | | David | 26 | 98 | +-------------+------------+------------------+ 4 rows in set (0.00 sec)
The following is the query to generate the serial number in MySQL query −
mysql> SELECT @serialNumber − = @serialNumber+1 yourSerialNumber, -> StudentName,StudentAge,StudentMathMarks from tblStudentInformation, -> (select @serialNumber − = 0) as serialNumber;
Here is the output displaying the row number in the form of serial number.
+------------------+-------------+------------+------------------+ | 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)
- Related Articles
- Count the occurrences of specific records (duplicate) in one MySQL query
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- MySQL query to ORDER BY records on the basis of modulus result
- MySQL query to alphabetize records and count the duplicates?
- MySQL query to get the count of distinct records in a column
- Display the count of duplicate records from a column in MySQL and order the result
- Returning a value even if there is no result in a MySQL query?
- MySQL query to display the count of distinct records from a column with duplicate records
- MySQL query to generate row index (rank) in SELECT statement?
- How to display the count from distinct records in the same row with MySQL?
- Get total in the last row of MySQL result?
- MySQL query to get records after an interval of 8 months
- Add a column count in a MySQL query on the basis of last name records?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- MySQL query to return multiple row records with AND & OR operator

Advertisements