Languages
[Edit]
EN

MySql - get row position with SELECT query

1 points
Created by:
Root-ssh
6376

This article is focused about problem how in MySql get row number with each row.

Quick solution:

SELECT 
	(@row_number := @row_number + 1) AS `row_number`,
	`client_id`,
	`creation_time`
FROM `clients`
JOIN (SELECT @row_number := 0) r

Screenshot:

row number with select query - HeidiSQL with MySql
row number with select query - HeidiSQL with MySql

Alternative solution

This approach creates variable outside query.

SET @row_number := 0;
SELECT 
	(@row_number := @row_number + 1) AS `row_number`,
	`client_id`,
	`creation_time`
FROM `clients`
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.