Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all ,
i need serial number in order by query in my sql. Means i have table like following(table)
C#
id   name
11   ram
21   sita
31   gita
41   deepak
51   harshit
61   prashant

my query is
C#
select name,@acount:=@acount+1 serial_number
from (SELECT @acount:= 0) AS acount,user
order by name asc

and i need result like this
C#
name     serial_number
deepak   1
gita     2
harshit  3
prashant 4
ram      5
sita     6

thanks
mohit
Posted
Updated 16-Sep-13 23:16pm
v2

Hi try like this,

This is the sample query i have written to achieve your requirement.
Hope it helps.

select concat(name,' ',@row := @row + 1) as Name from user,(SELECT @row := 0) r order by @row := @row;
select name as Name,@row := @row + 1 as SNo from user,(SELECT @row := 0) r order by @row := @row;
 
Share this answer
 
v2
Comments
mohit.a44 17-Sep-13 5:15am    
i need this with order by
Thomas ktg 17-Sep-13 5:23am    
order by name or row no?
mohit.a44 17-Sep-13 5:31am    
name not row, and row no will come according to name
Thomas ktg 17-Sep-13 5:33am    
i updated the solution please look at it.
mohit.a44 17-Sep-13 5:35am    
wrong your solution is giving me this

Name SNo
ram 1
sita 2
gita 3
deepak 4
harshit 5
prashant 6

and i need this

name serial_number
deepak 1
gita 2
harshit 3
prashant 4
ram 5
sita 6
SQL
select name,row_number() over(order by Id) as serial_number from user
 
Share this answer
 
Comments
Member 11023380 21-Aug-14 5:43am    
its not working
Member 11023380 21-Aug-14 5:44am    
its not working in ms access 2007
i used other way limit 0,10 then 11,10 so on....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900