Guy
Guy

Reputation: 876

Select after first 50 with MYSQL and PHP

I need to select all the rows AFTER the first 50 rows from a table. I can't seem to figure out an efficient way to do it.

I could select the first 50 rows, put their IDs in an array, then make another query that selects everything except them, but that seems wasteful.

I can't seem to find anything here that does what I need:

http://www.w3schools.com/sql/default.asp

Is there anyway to do this in one SQL query? Thanks for the help!

Upvotes: 1

Views: 537

Answers (4)

hariK
hariK

Reputation: 3059

DELIMITER //

CREATE PROCEDURE displayDataFromGivenRow(IN from_Id INT)

BEGIN
DECLARE row_count INT;

SELECT count(*) INTO row_count
FROM <table_name>;

SELECT * 
FROM <table_name>
LIMIT row_count
OFFSET from_Id;

END//

DELIMITER ;

CALL displayDataFromGivenRow(50);

OR

SELECT * FROM table_name LIMIT total_number_of_records OFFSET 50;

Upvotes: 0

fkerber
fkerber

Reputation: 1042

You are probably looking for OFFSET:

For example:

  SELECT * FROM users ORDER BY id LIMIT 100 OFFSET 50

Upvotes: 7

jasim
jasim

Reputation: 288

Try this select * from tablename limit 50,100000000 the second argument in limit you can set based on number of row your table may have

Upvotes: -1

thomas.mc.work
thomas.mc.work

Reputation: 6474

You can define an offset:

[LIMIT {[offset,] row_count | row_count OFFSET offset}]

Like this:

SELECT * FROM table1 LIMIT 5, 10; # Retrieve rows 6-15

Upvotes: 2

Related Questions