Forum Moderators: phranque

Message Too Old, No Replies

Numbering rows in mysql

         

mcavic

2:55 pm on Jun 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does anyone know of a function in MySQL that will number the rows in a table dynamically upon a select statement? For example, something like:

select field1, rownum() from mytable;


field1 rownum
------ ------
50 1
25 2
77 3
333 4

The reason I want this is so that I can say:

select avg(field1) from mytable where rownum <= 5;

And it'll average the first 5 rows. Or is there another way to do the average?

Nutter

9:00 pm on Jun 19, 2004 (gmt 0)

10+ Year Member



What about
SELECT AVG(field) FROM table LIMIT 5
then...
SELECT AVG(field) FROM table LIMIT 5, 5
for the next 5, etc?

- Ryan

mcavic

3:24 am on Jun 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried that, but it doesn't seem to work. It evaluates the Limit only after the rest of the statement has been executed.

grandpa

6:28 am on Jun 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could try this,

select field1 from mytable where x is y;

With the right select you read 5 rows in a while loop,
get the average, and read 5 more.

mcavic

8:45 pm on Jun 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, I can calculate the average myself in a loop. Oracle has a function that'll do it with just one select.

gmiller

12:48 am on Jun 25, 2004 (gmt 0)

10+ Year Member



Don't newer versions of MySQL have support for subselects?