All pastes #210939 Raw Edit

Mine

public text v1 · immutable
#210939 ·published 2006-10-20 02:39 UTC
rendered paste body
I have two tables:

#Favorites#
content_type_id (INT)
object_id (INT)
^unique together^

#ChangeLog#
content_type_id (INT)
object_id (INT)
<some other fields>
change_time (DATETIME)

I want to select all from favorites, and join them with changelog, order the changelog descending, and then group it.

Here's my solution?

SELECT *
FROM (

SELECT `django_history_log`. *
FROM `favorites_favorite` , `django_history_log`
WHERE `favorites_favorite`.`content_type_id` = `django_history_log`.`content_type_id`
AND `favorites_favorite`.`object_id` = `django_history_log`.`object_id`
ORDER BY change_time DESC
LIMIT 50
)t2
GROUP BY `content_type_id` , `object_id`
LIMIT 0 , 30