37

I have a table: student_marks

marks
-----
  44
  55
  64
  98
  76

Expected output:

serial_number|marks
--------------------
  1          | 44
  2          | 55
  3          | 64
  4          | 98
  5          | 76

Using mysql user defined variables, it could be done using query:

 set  @a:=0;select @a:=@a+1 serial_number, marks from student_marks;

Is there any way to achieve this in msyql without using user defined variables?

3
  • 1
    possible duplicate of MySQL - row number in recordset?
    – Ja͢ck
    Jun 19, 2012 at 4:42
  • Why don't you want to use user defined variables?
    – GarethD
    Jun 19, 2012 at 6:09
  • I have to use two queries, one for initializing the mysql variable to 0 and another the actual query. I was trying to avoid two queries here.
    – sushil
    Jun 19, 2012 at 6:13

13 Answers 13

76

Based on your reasons for not wanting to use user defined variables as wanting to avoid having 2 queries, one for inializing and one to use it you could use the following:

SELECT  @a:=@a+1 serial_number, 
        marks 
FROM    student_marks,
        (SELECT @a:= 0) AS a;
8
  • I wonder, how this works without AS keyword in SELECT statement?
    – xkeshav
    May 9, 2013 at 5:45
  • 2
    @diEcho The MySQL Docs state The AS keyword is optional when aliasing a select_expr with an identifier. See this working example without AS on SQL Fiddle
    – GarethD
    May 9, 2013 at 7:48
  • What if I want to loop through not just numbers but also all upper and lower case letters (i.e. creating a base 62 number sequence), and have it all left-padded with 0's? :D Oct 22, 2015 at 19:15
  • 1
    I suggest use of an explicit cross join instead. i.e. FROM student_marks CROSS JOIN (SELECT @a:= 0) AS a; so the intention is clear. Oct 26, 2015 at 1:05
  • 1
    @NullPointer You can also INNER JOIN to the query that initialises the variable, e.g. SELECT @a:=@a+1 AS serial_number FROM T1 INNER JOIN T2 ON T2.ID = T1.ID INNER JOIN (SELECT @a:= 0) AS a; - sqlfiddle.com/#!9/f155b4/1
    – GarethD
    Jan 24, 2018 at 13:16
11

Best Answer to this question should be the following, the best practice

SET @count:=0;
SELECT  (@count:=@count+1) AS serial_number, 
        marks 
FROM    student_marks
5
  • Why did you reproduce the same answer as the accepted one from 2012?
    – Veve
    Aug 31, 2017 at 11:48
  • 2
    This is not same answer, moreover it quit faster than @GarethD's solution
    – Naveen DA
    Oct 9, 2017 at 3:44
  • Just bad wording: he should have either suggested it as an alternative, or cited his evidence for "best practice". No reason to get angry or excited.
    – Parapluie
    Feb 23, 2018 at 11:19
  • 1
    This is not the best answer to the question, since the OP specifically wanted a solution that uses one statement, not two. Moreover, the OP has already posted this solution as part of the question. So while may be a better approach if you can use more than one statement, it is not the best answer to this question.
    – GarethD
    Jul 13, 2018 at 13:17
  • Note: This number won't be in sequence if you have ORDER BY
    – xjlin0
    Jun 9, 2022 at 0:21
1

I had a table with a column(c5) has contain a number x, I need a sql expression that repeated the same row x numbers of times:

my table A contains:

c1  c2  c3  c4  c5
16  1   2   16  3
16  1   2   17  2 
16  1   2   18  1

and I need:

c1  c2  c3  c4  c5  n
16  1   2   16  3   1
16  1   2   16  3   2
16  1   2   16  3   3
16  1   2   17  2   1
16  1   2   17  2   2
16  1   2   18  1   1

I solved That with the expression:

SELECT
    c1, c2, c3, c4, c5, row_number AS n
FROM
    (
        SELECT
            @curRow := @curRow + 1 AS row_number
        FROM
            tablea
        JOIN (SELECT @curRow := 0) r
        WHERE
            @curRow < (
                SELECT
                    max(field1)
                FROM
                    tablea
            )
    ) AS vwtable2
LEFT JOIN tablea d ON vwtable2.row_number <= tablea.field1;
1

tested in mysql 5.6:

SELECT
    @a:=@a+1 as serial_number,
    student.* 
FROM
    student
join (SELECT @a:= 0) a
0
1

you can add serial number with this query :

set @counter=0;
SELECT  @counter:=@counter+1 as serial_number from <table_name>

output :

   |...............................|
   | serial_number  |   first_name |
   |...............................|
   |      1         |    mojtaba   |
   |      2         |    athena    |
   |      3         |     azin     |
   |      4         |     ava      |
   |      5         |    arash     |
   |...............................|

in addition if you want to use in where

set @counter=0;
select serial_number from (SELECT  @counter:=@counter+1 as serial_number 
from <table_name>) as tbl_counter where serial_number > 3

output :

   |...............................|
   | serial_number  |   first_name |
   |...............................|
   |      4         |     ava      |
   |      5         |    arash     |
   |...............................|
1

I found simple way to generate serial number

select ROW_NUMBER() over (PARTITION by '1') AS serial_number,customer_id from users;

Also it support pagination

1
  • simplest one, however it existed in 2022 (supported by mysql 8), did not exist in earlier versions
    – Sami
    Aug 17 at 8:45
0

No, there isn't. But you could produce the serial_number in program side, not the database side.

1
  • Yes, that can be done. I have already generated serial number using mysql user defined variables as well. I was wondering if there are some alternate ways in mysql to achieve this.
    – sushil
    Jun 19, 2012 at 4:54
0

None of the above answers seemed satisfactory as you would need to initialize the variable as null in a separate query.

So posting the below all encompassing solution:

SELECT ROUND(@a:=@a+1,0) sr_num, t.* from (select @a:=0) initvars, your_table t;
0

@a:=@a+1 serial_no,

CROSS JOIN (SELECT @a:= 0) AS a

SELECT @a:=@a+1 serial_no, sc.id, sc.name, sc.address, c.country,s.state 
FROM oc_table1 sc CROSS JOIN (SELECT @a:= 0) AS a LEFT JOIN oc_table2 .......
0

ALTER TABLE table_name ADD COLUMN sr_no INT UNIQUE NOT NULL AUTO_INCREMENT FIRST;

This will even edit your main table in case you forgot to create a column for serial numbers. You can also use this to add sr_no column to two different columns and easily join them (or create a new table using both of them).

-1

Why don't you make it an auto-increment field?

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

1
  • For simplicity of asking question, I created this table. Actually, I have to add a serial number to a result which I will get by joining two tables.
    – sushil
    Jun 19, 2012 at 4:52
-1

No, unless you have a column which containes these numbers.

-1

Though very late for the answer, generating the sequential ids without having two queries and without using joins and without using sub query.

SELECT *, (@cnt := if(@cnt IS NULL, 0,  @cnt) + 1) AS id FROM table_name;

Cheers

1
  • When executed multiple times, the cnt doesnt reset to 0 Mar 10, 2016 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.