tags:

views:

50

answers:

3

How would I select all but the first 3 rows in my MySql query?

$query = mysql_query("SELECT * FROM comments WHERE approved = 1");  
+2  A: 

Do you want the following:

$query = mysql_query("SELECT * FROM comments WHERE approved = 1");
$rowCount = 0;
while ($row = mysql_fetch_assoc($query)) {
    $rowCount = $rowCount + 1;
    // do stuff only if you have reached the third row.
    if ($rowCount > 3){
        // do stuff here
    }
}
phimuemue
+2  A: 
SELECT * FROM comments WHERE approved = 1 LIMIT 3,SOME_HUGE_NUMBER

See this post for more info

Amarghosh
Shouldn't it be LIMIT 3, SOME_HUGE_NUMBER? Yours gives all but the first 2.
Matthew Flaschen
@Matthew [The offset of the initial row is 0 (not 1)](http://dev.mysql.com/doc/refman/5.1/en/select.html#id3294320)
Amarghosh
@Amarghosh, right. Which means you're starting at the third row (0, 1, 2). He said all *but* the first three, which means starting at the fourth (3).
Matthew Flaschen
@Matthew yup, fixed. Thanks :)
Amarghosh
A: 
$query = mysql_query("SELECT * FROM comments
                                     WHERE approved = 1 
                                     ORDER BY columnName 
                                     LIMIT 3");

To find third record order by columnName use

$query = mysql_query("SELECT * FROM comments
                                     WHERE approved = 1 
                                     ORDER BY columnName 
                                     LIMIT 2, 1");

To find All other than 1st 3 rows use LIMIT 2, total_no_of_rows

if you don't know total_no_of_rows use very large number instead of it.

$query = mysql_query("SELECT * FROM comments
                                     WHERE approved = 1 
                                     ORDER BY columnName 
                                     LIMIT 2, total_no_of_rows");
Salil
@Flexi Kling:- Yes, I edit my answer now.
Salil
This is all but the first *2*.
Matthew Flaschen