Unix Technical Forum

Incrementally number result rows

This is a discussion on Incrementally number result rows within the MySQL forums, part of the Database Server Software category; --> Hello group, How would I go about numbering rows from an SQL query? For example, if I use the ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:29 AM
Max
 
Posts: n/a
Default Incrementally number result rows

Hello group,

How would I go about numbering rows from an SQL query?

For example, if I use the query:

SELECT myCol FROM myTable ORDER BY myOtherCol;

it returns:

+-------+
| row x |
| row y |
| row z |
+-------+

Now I would like to get:

SELECT [something], myCol FROM myTable ORDER BY myOtherCol;
+-----------+
| 1 | row x |
| 2 | row y |
| 3 | row z |
+-----------+

....or...

SELECT [something], myCol FROM myTable ORDER BY myOtherCol DESC;
+-----------+
| 1 | row z |
| 2 | row y |
| 3 | row x |
+-----------+



Thanks,
Max

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:29 AM
Max
 
Posts: n/a
Default Re: Incrementally number result rows

On Oct 26, 9:15 am, Max <Maxime.Pla...@gmail.com> wrote:
> Hello group,
>
> How would I go about numbering rows from an SQL query?
>
> For example, if I use the query:
>
> SELECT myCol FROM myTable ORDER BY myOtherCol;
>
> it returns:
>
> +-------+
> | row x |
> | row y |
> | row z |
> +-------+
>
> Now I would like to get:
>
> SELECT [something], myCol FROM myTable ORDER BY myOtherCol;
> +-----------+
> | 1 | row x |
> | 2 | row y |
> | 3 | row z |
> +-----------+
>
> ...or...
>
> SELECT [something], myCol FROM myTable ORDER BY myOtherCol DESC;
> +-----------+
> | 1 | row z |
> | 2 | row y |
> | 3 | row x |
> +-----------+
>
> Thanks,
> Max


Answer to myself (I hope it can help others)
taken from: http://forums.mysql.com/read.php?10,36490,36511

SET @row = 0;
SELECT @row := @row + 1 AS rowNumber, myCol FROM myTable ORDER BY
myOtherCol DESC;

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:25 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com