views:

141

answers:

4

I want to SELECT all rows except for the first 5 rows in a table.

How do I do that?

Why cant I just type

$query = "SELECT * FROM ages OFFSET 5 ORDER BY id ASC";

A: 

In Oracle:

select name, price from items where rownum > 5

Koder_
http://www.adp-gmbh.ch/ora/sql/rownum.html
Koder_
Oh, your dbms is mysql sorry.
Koder_
+6  A: 

SELECT * FROM tbl LIMIT 5,18446744073709551615;

from http://dev.mysql.com/doc/refman/5.0/en/select.html

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

jerjer
shame on mysql for using magic numbers w/o documentation. might as well use 3141592653589793238
Jason S
It's not really magic. It's just any number greater than the number of rows in the table.
recursive
@jason: That returns zero rows. I tested it.
Asaph
A: 

I just typed:

    $query = "SELECT *
            FROM ages
            LIMIT 100
            OFFSET 10";

Why couldn't anybody give me such an easy answer? :)

weng
The MySQL documentation only says it's for compatibility, but it is exactly what you're looking for according to the Postgres documentation: http://www.postgresql.org/docs/7.3/static/queries-limit.html
OMG Ponies
No one gave you "such an easy answer" because you wanted to return *all* rows except the first 5 (10 in your answer). The "easy answer" you posted will only return 90 rows after skipping the first 10. If there are more than 100 rows in the table, your "easy answer" fails to return them.
Asaph
A: 

Here's a solution using variables - just add your order by clause and you should be set.

set @n=-1
select * from TABLE where (@n:=@n+1) >= 5;
Eric Holmberg
See my answer for how to perform that in a single statement.
OMG Ponies
Nice way to initialize the variable. Do you have any idea if there is a performance hit with your solution?
Eric Holmberg