mysql查询明显的desc顺序

时间:2011-07-14 02:19:31

标签: php mysql cakephp

为什么我无法获得total_time_driven_at_this_trip的描述顺序?

    SELECT DISTINCT (`name`), 
           MAX( `total_time_driven_at_this_trip` ) as trip
     FROM `users` 
LEFT JOIN `trip_nsws` ON users.id = trip_nsws.user_id
 GROUP BY `user_id`
 ORDER BY `total_time_driven_at_this_trip` DESC
    LIMIT 0 , 30

2 个答案:

答案 0 :(得分:2)

结果集中没有用于排序的列total_time_driven_at_this_trip

只有DISTINCT(name)trip

您可能想要ORDER BY trip DESC

答案 1 :(得分:2)

因为您将其别名为trip并使用GROUP BY

SELECT `name`, MAX(`total_time_driven_at_this_trip`) as trip
FROM `users` LEFT JOIN `trip_nsws` ON users.id = trip_nsws.user_id
GROUP BY `user_id`
ORDER BY `trip` DESC
LIMIT 0, 30