tags:

views:

26

answers:

3

I have a query that selects the nth results (in this case the 10th) I just want to use the reply_chunk_id of that nth result and count others like it to see how many there are in total with the same id. Does anyone know how to do this in one query?

SELECT reply_chunk_id,message 
FROM (
    SELECT * 
    FROM messages
    ORDER BY timestamp ASC
    LIMIT 10
) AS tbl WHERE topic_id=?
ORDER BY timestamp DESC
LIMIT 1
A: 

use LIMIT 1 OFFSET 9 maybe. Not sure this is built in MySQL.

Benoit
I want to count the rows with the same id as the one that is shown as the nth result, I am not sure what you are saying, I already have the correct nth result chosen, I just want to count ids like its own
Scarface
Sorry, I have not understood the question properly.
Benoit
+3  A: 

You can select the 10th row by

SELECT reply_chunk_id FROM messages ORDER BY timestamp ASC LIMIT 9,1

so

SELECT COUNT(*) FROM messages
   WHERE reply_chunk_id = (SELECT reply_chunk_id FROM messages
                              ORDER BY timestamp ASC LIMIT 9,1)
     AND topic_id = ?
KennyTM
thanks kenny, to display the number of results I changed your count(*) to COUNT(reply_chunk_id) and used $row2['COUNT(reply_chunk_id)']; to display it. Is that the proper way of doing it?
Scarface
@Scarface: It's fine. You could also give it a name e.g `select count(reply_chunk_id) as the_count from ...`.
KennyTM
thanks again kenny, appreciate it
Scarface
A: 
SELECT COUNT(1)
FROM messages
WHERE reply_chunk_id =
(SELECT MIN(reply_chunk_id)
 FROM messages
 WHERE timestamp =
    (SELECT MAX(timestamp)
     FROM   (SELECT timestamp
             FROM   messages
             ORDER BY timestamp ASC
             LIMIT 10)))
Andrew