Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate serial number in mysql query

Tags:

sql

mysql

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?

like image 778
sushil Avatar asked Jun 19 '12 04:06

sushil


People also ask

How do I find the serial number of a MySQL query?

To generate serial number i.e. row count in MySQL query, use the following syntax. mysql> select *from tblStudentInformation; The following is the output.

How do you add a serial number to an Access query?

Then add a textbox control in this subform and set its Control Source to =SerialNumber([Form]) . That would fulfill all your needs. Remark: If you delete a record in the subform you would have to refresh the subform to update the serial numbering.

What is serial MySQL?

SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE . SERIAL DEFAULT VALUE in the definition of an integer column is an alias for NOT NULL AUTO_INCREMENT UNIQUE .

Is there a profiler for MySQL?

Query Profiler, built into dbForge Studio for MySQL, is the best query optimization tool to tune MySQL queries and investigate query performance issues in an efficient and fast way. It helps build up a picture of how the queries are run to access data and what operations impact your application.


1 Answers

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; 
like image 94
GarethD Avatar answered Sep 24 '22 02:09

GarethD