PDA

View Full Version : Resolved Getting the WHERE clause to return a range


thresher
01-22-2010, 12:42 PM
Firstly, I am a real MySQL noob, so sorry if this is really obvious!

I'm trying to get a page to display a list of items from a database, but only display 6 of those items per page.

I can get the mysql to return all the items or one of the items, but I'm trying to get it to just return the range I specify per page.

So on page 1 I want items 1-6 to show, so I've used the following code:


$sql = "SELECT unit_id, company_name, website, contact_name, phone_no, description
FROM details
WHERE unit_id='1'
ORDER BY unit_id";


Obviously this only displays the item 'unit_id 1'.

Is there a clause I can add to the WHERE clause to say "Get all items between unit_id 1 and 6"? Or am I using the wrong clause altogether?

djm0219
01-22-2010, 12:50 PM
$sql = "SELECT unit_id, company_name, website, contact_name, phone_no, description
FROM details
ORDER BY unit_id"
LIMIT 6;

thresher
01-22-2010, 01:36 PM
Thanks djm, but LIMIT is not really what I'm after. I want to specifically return unit_id 1 through 6, not just the first 6.

abduraooft
01-22-2010, 01:53 PM
Is there a clause I can add to the WHERE clause to say "Get all items between unit_id 1 and 6"? Or am I using the wrong clause altogether?
It's as straight as $sql = "SELECT unit_id, company_name, website, contact_name, phone_no, description
FROM details
WHERE unit_id between 1 and 6
ORDER BY unit_id";

thresher
01-22-2010, 02:31 PM
It's as straight as $sql = "SELECT unit_id, company_name, website, contact_name, phone_no, description
FROM details
WHERE unit_id between 1 and 6
ORDER BY unit_id";


Typical! Thanks abduraooft!
I found another method as well, in the form of:


$sql = "SELECT unit_id, company_name, website, contact_name, phone_no, description
FROM details
ORDER BY unit_id
LIMIT 4, 6";