tags:

views:

77

answers:

4

If I specify a number, say 5, what query will give me all the rows after the 5th row? Like,

SELECT * FROM table WHERE 1=1;

only I want it to exclude the top 5. Thanks.

+5  A: 

Use the limit with a very high number as the second argument.

select * from myTable limit 5,18446744073709551615;

From MySQL Docs:

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;

BTW: You don't need WHERE 1=1. It doesn't add any value to the query, just clutter.

Asaph
Yeah, that WHERE was just placeholder. This should do it. Thanks!
mathon12
+4  A: 
SELECT * FROM table ORDER BY somecolumn LIMIT 5,1000

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

[LIMIT {[offset,] row_count | row_count OFFSET offset}]
gahooa
+1 for specifying the order
dave
This query won't retrieve anything past row 1000. What if the table contains more than 1000 rows?
Asaph
@Asaph: how many rows do you want to retrieve? I included specification of the LIMIT syntax - if this isn't obvious, then perhaps one should move to a different field?
gahooa
A: 

Are you looking for LIMIT?

SELECT * FROM table LIMIT 5,9999999

The second parameter to limit is just a large number to get all rows. Adjust accordingly.

See: http://dev.mysql.com/doc/refman/5.5/en/select.html

ZoogieZork
A: 

If you have a column whose values can be ordered (and are unique), say ID1, You can do it in pure SQL (e.g. not using the MySQL specific LIMIT) as follows (Syntax is Sybas-ey, may need to tweak table alias and joins to work on mySQL):

SELECT * FROM table WHERE ID1 not in
-- SELECT FIRST @N ROWS IN ACCENDNING ORDER
(SELECT t1.ID1 FROM table 't1', table 't2' 
 WHERE t1.ID1 < t2.ID2
 GROUP BY t1.ID1
 HAVING count(*) <= @N)
DVK
Interesting answer. However, with MySQL un-optimization of sub queries, you are likely to give the CPU a heart attack.
gahooa
OK, so stick the IDs from sub-query into a temp table for the unoptimized databases :)
DVK