I have to write a complex query.
I have mysql table name ads which has 1000 ads.
Right now my pagination showing all ads in descending order by date added column. 20 records per page.
Select * from ads order by sort_order asc,date_added desc LIMIT 0,20;
New Requirement is
sort_order is assigned base on membership type. So on every page I need to show 20 ads total (5 ads from each membership type). If a membership does not have 5 records on any page next membership will show 5+missing-ads from previous membership.
Now I need order of My page like this
Name date_added sort_order
ad1 12-2-2015 1 //PREMIUM MEMBER's ADS
ad2 11-2-2015 1 //PREMIUM MEMBER's ADS
ad3 10-2-2015 1 //PREMIUM MEMBER's ADS
ad4 09-2-2015 1 //PREMIUM MEMBER's ADS
ad5 08-2-2015 1 //PREMIUM MEMBER's ADS
ad6 13-2-2015 2 //GOLD MEMBER's ADS
ad7 12-2-2015 2 //GOLD MEMBER's ADS
ad8 07-2-2015 2 //GOLD MEMBER's ADS (There may be chance gold type of mebers has posted only 3 ads)
ad9 18-2-2015 3 //SILVER MEMBER's ADS
ad10 10-2-2015 3 //SILVER MEMBER's ADS
ad11 03-2-2015 3 //SILVER MEMBER's ADS
ad12 02-2-2015 3 //SILVER MEMBER's ADS
ad13 02-2-2015 3 //SILVER MEMBER's ADS
ad14 02-2-2015 3 //SILVER MEMBER's ADS
ad15 01-2-2015 3 //SILVER MEMBER's ADS
ad16 12-2-2015 4 //REGULAR MEMBER's ADS
ad17 08-2-2015 4 //REGULAR MEMBER's ADS
ad18 03-2-2015 4 //REGULAR MEMBER's ADS
ad19 02-2-2015 4 //REGULAR MEMBER's ADS
ad20 01-2-2015 4 //REGULAR MEMBER's ADS
Means if intermediate rows not exist with particular sort_order next order should show more records. As in above example sort_order=2 has 3 records in table so sort_order=3 should show 7. 5+2=7 Also need to keep order by date added in descending for a particular sort order.
Can this be done with single mysql query so my pagination does not stop working. ?
Or any other way.
I have tried union all but that does not work for missing records.
Your help will be appreciated. As I am trying it from last 2 days. Now I decided to ask here.