Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numbering Rows

Status
Not open for further replies.

boy007

Programmer
Mar 28, 2006
3
GB
I have a couple of tables that I query, but I wish to add an extra column in the query which numbers the result set from 0-* (note, I don't wish to find out how many results there are).

This is so that I can extract rows between two numbers for example:

...where rowcount BETWEEN 32 AND 40

and I'd get a result such as

rowcount column 1
32 name1
33 name2
34 name3
35 name4
etc.


I'm using MySql 3.x

Thanks.
 
sorry, no can do the numbering

however, you might be interested in the LIMIT keyword in the SELECT statement

r937.com | rudy.ca
 
So, you want a ranking query ?
You should have a unique sort key in order to do this.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The following SQL works for me in SQL Server - it might need tweaking to make it ANSI:
Code:
select staff_no, surname, rank = (select count(*) from Employees where staff_No < E.Staff_No)
from Employees E
ORDER BY Staff_No

Obviously Staff_No is a key field.
 
Thanks guys. Valuable info gained, but doesn't look like I can get round this one in MySQL
 
cheerio's query does work in mysql -- in the current version, in the version before that, and also in the version before that

you're on the great-grandfather version :)


r937.com | rudy.ca
 
I guess the correct syntax is:
select staff_no, surname, (select count(*) from Employees where staff_No < E.Staff_No) rank
from Employees E
ORDER BY Staff_No

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
oh yeah, thanks PHV, i overlooked the fact that cheerio's query used rank=(select...

which obvioulsy is sql server syntax

i was referring to the use of a subquery, which mysql does support starting with verion 4.1 (the grandfather of the current version)

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top