I have a database with the following structure
- Items table: itemID, itemName
- Ads table: AdsId, itemID
I would like to make pagination on items table, 10 items in each page, but first I must retrieve items whose IDs are in the Ads table, and then retrieve other items.
I know I must use limit
such as:
SELECT * FROM Items LIMIT $offset, $no_of_records_per_page
And may I join
ed it with adsTable, such as:
SELECT * FROM Items inner join Ads on Ads.itemID = Items.itemID
LIMIT $offset, $no_of_records_per_page
but how can I achieve what I described?
Thanks in advance.
Best Answer
METHOD #1
You could create a temp table called
itemID_paginator
and paginate thisYou then paginate against
itemID_paginator
and join it to AdsMETHOD #2
If you prefer not to use a temp table, then perform
LIMIT
directly to theItems
tableI suggested these methods before in the following posts:
Jan 31, 2017
: Slow query while selecting a higher offset from millions of rowsDec 21, 2013
: Using OFFSET in a large table by a simple sub-queryFeb 21, 2013
: Does MYSQL join before LIMIT?May 16, 2011
: Fetching a Single Row from Join TableGIVE IT A TRY !!!