0
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)

2 Answers 2

2

Order by image_id after the join.

(You do have an index on image_id, don't you?)

UPDATE:

You can put the ORDER BY inside your GROUP_CONCAT:

GROUP_CONCAT(image_id ORDER BY image_id)

and then you don't need the subquery.

5
  • :) not a solution. Order should be used for different table field.
    – Somebody
    Apr 26, 2011 at 15:22
  • Sure, it's primary key and ad_id as well have index.
    – Somebody
    Apr 26, 2011 at 15:25
  • Cool! I didn't knew that! Thanks mate. +1 :)
    – Somebody
    Apr 26, 2011 at 15:36
  • Glad to help. And I just gained a new privilege... yay!
    – awm
    Apr 26, 2011 at 15:38
  • Rofl :D Baaaah! Solution is working, but I just noticed it doesn't order the rest GROUP_CONCAT fields. I need to use it in each field. Ok ty anyway :)
    – Somebody
    Apr 26, 2011 at 15:41
1

It's not clear in this example why you need to order the subquery, so I'd say just get rid of the ordering frmo the subquery and only apply ordering on the outermost query.

6
  • because of group_concat(i.image_id) :) There a multiple rows inside joined table
    – Somebody
    Apr 26, 2011 at 15:22
  • 1
    @Beck: Then maybe you should post the whole query. Apr 26, 2011 at 15:25
  • Sure sorry, it's my laziness and bad habit. Just a moment.
    – Somebody
    Apr 26, 2011 at 15:27
  • @Beck: I don't have MySQL here to test with but have you tried adding a third outer query that just does the group_concat and ordering on the result of your current outer query (minus the grouping and ordering of course)? Apr 26, 2011 at 15:32
  • Please add an example of modified query and i'll launch it here. :)
    – Somebody
    Apr 26, 2011 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.