RIGHT JOIN (SELECT * FROM images ORDER BY image_id) AS `i` ON i.ad_id = a.id
How to order joined table without using temporary table? 🙂
Because it’s 5x slower than simple join:
RIGHT JOIN images AS `i` ON i.ad_id = a.id
And there are problems with fields list, which should be duplicated inside subquery as well to avoid using * sign.
Thanks 😉
Update
SELECT a.id, GROUP_CONCAT(image_id) AS `image_ids`
FROM `ads` AS `a`
RIGHT JOIN (SELECT * FROM images ORDER BY image_id) AS `i` ON i.ad_id = a.id
GROUP BY `a`.`id`
ORDER BY `a`.`id` DESC
There are multiple rows that match per id in joined table. That’s why i need to order them.
group_concat(image_id)
Order by image_id after the join.
(You do have an index on image_id, don’t you?)
UPDATE:
You can put the
ORDER BYinside yourGROUP_CONCAT:and then you don’t need the subquery.