tags:

views:

212

answers:

3

I want to query a database and return specific row numbers eg 1, 10 and 25.

Is this possible or will I need to do 3 queries (or 1 query and loop through the rows then only output the ones I need)

using php.

A: 

you need to use the limit clause:

for first row:

 select * from table limit 0, 1

for tenth row:

 select * from table limit 9, 1

for 25th row:

 select * from table limit 24, 1  

SOLUTION WITH JUST ONE QUEERY:

If you are under php, you can use: mysql_fetch_row:

<?php
$result = mysql_query(" select * from table");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 1st
echo $row[9]; // 10th
    echo $row[24]; // 25th
?>
Sarfraz
I was actually asking if it's possible to get the 3 rows in 1 query ratherthan use 3 seperate queries.
Hintswen
@Hintswen: You can use a `UNION` to combine those three queries if necessary.
Peter Lang
@Hintswen: if you are under php, you can use one query and then use mysql_fetch_row function of mysql.
Sarfraz
@Hintswen: PLZ SEE MY ANSWER AGAIN
Sarfraz
Just saw the rest of your answer. This will probably be the way I do it if I don't get. Better solution.
Hintswen
The problem with your 1 query answer is it needs to send all that data across the network (I won't always be queryig from the same server)
Hintswen
The mysql_fetch_row() part is incorrect. $row = mysql_fetch_row(); gives you a single row and its columns, not all rows. You would have to fetch *all* rows and then get the ones you need, but it would be quite inefficient.
Joel L
+4  A: 

If there are specific IDs you require, you could simply use something along the lines of..

SELECT XXX FROM table WHERE XXX_id IN (1, 10, 25) ORDER BY XXX

...to get these rows back. If however you're just after arbitrary row numbers, then you'll have to use multiple queries as far as I'm aware. (Or use PHP, etc. to pluck the required rows back.)

Out of interest what are you trying to achieve?

middaparka
I have 1 record per minute, I was thinking of getting the rows for specific times eg letest record, 60 minutes ago, 1 day ago, 7 days ago.
Hintswen
Don't you have a datestamp field or something you could use? You could simply have a where clause that selected (condition one OR condition two OR condition three), etc.
middaparka
I'm storing the time as an integer but it's not esactly 60 seconds between rows due to calculation time. I was going to ask how to find the row with the time value closest to a number aswell.
Hintswen
If you retrieve/normalise the time prior to the calculation (and associated DB storage) you could reduce this is a simple where query as I suggested - this might make things a lot simpler in the long run.
middaparka
I would rather not alter the timestamps as the data in the row is specific to the time it was calculated (even down to the second) although it might be better in the long run.I might end up doing this then.
Hintswen
+1  A: 

If you need to do this often (and if the db is large), you have to also store the row numbers (more specifically - row id to number mappings) somewhere.

MyTable
-------
id
row_nr
...

and update the row_nr's every time you remove an entry.

i.e. - if you remove an entry with the id=10, you have to

UPDATE MyTable SET row_nr = row_nr - 1 WHERE id > 10

when adding rows use

INSERT INTO MyTable (whatever, row_nr)
VALUES ('thatever', (
SELECT MAX(MT2.row_nr)+1 FROM MyTable as MT2
))

--

Then you can use

SELECT XXX FROM MyTable WHERE row_nr IN (1, 10, 25)

when selecting things.

Joel L
if u am filtering my results before selecting the row number then the number column would have to be updated after the filter right?
Hintswen