For the past 4 hours I've been laser focused on this one problem, in a nut shell, I want to order this table by id in DESC order, grouped by ads_post_id (in DESC order based on id), with a LIMIT of 6 rows returned.
Sample of database,
id | ads_post_id
---------------------------------------------------------------------------
22 | 983314845117571
23 | 983314845117571
24 | 983314845117571
104 | 983314845117571
250 | 983314845117571
253 | 983314845117571
767 | 983314845117571
---------------------------------------------------------------------------
My current query,
SELECT * FROM fb_ads GROUP BY ads_post_id ORDER BY id DESC LIMIT 6
However all this returns is,
id | ads_post_id
---------------------------------------------------------------------------
22 | 983314845117571
---------------------------------------------------------------------------
It should return,
id | ads_post_id
---------------------------------------------------------------------------
767 | 983314845117571
---------------------------------------------------------------------------
So clearly it's been grouped in ASC order and then ordered by ID in DESC order right?
So this has led me down a rabbit hole with research, most people seemed to use this as a work around, but it's not preferable because of the performance hit, this query needs to be recalled every time a user goes onto a next page,
SELECT * FROM
(
select * from fb_ads order by id desc
) as fb_ads
group by ads_post_id
order by id DESC LIMIT 6
HOWEVER, it still didn't work for me, this only returned,
---------------------------------------------------------------------------
id | ads_post_id
---------------------------------------------------------------------------
22 | 983314845117571
---------------------------------------------------------------------------
PLEASE NOTE: This is a sample of my database for simplicity of answering, in practice there will be thousands of ads_post_id
, so as far as I know at this time MYSQL's MAX()
function won't work because it only returns one row.
I'm not an expert in MYSQL, but I know enough to get around, I feel this needs a solution outside my scope of expertise.
Some help would go a very long way, thank you.