MYSQL按顺序排序

时间:2018-02-17 13:21:04

标签: mysql sql

我需要帮助mysql顺序,我有这个查询

SELECT
  videos_views.videos_views_id,
  videos_views.videos_views_date,
  SUM(IF(MONTH(videos_views.videos_views_date) = 2, videos_views.videos_views_total, 0)) AS total_view,
  videos.videos_id,
  videos.videos_title,
  videos.videos_description,
  videos.videos_author_list_id,
  author_list.author_list_name,
  author_list.author_list_id
FROM videos
  LEFT OUTER JOIN author_list
    ON videos.videos_author_list_id = author_list.author_list_id
  LEFT OUTER JOIN videos_views
    ON videos.videos_id = videos_views.videos_views_id
WHERE author_list.author_list_video_type = 1
AND videos.videos_id >= 51108
GROUP BY videos_views.videos_views_id,
         videos.videos_id
ORDER BY CASE WHEN MONTH(videos_views.videos_views_date) = 2 THEN SUM(videos_views.videos_views_total) END DESC, 
  CASE WHEN MONTH(videos_views.videos_views_date) <> 2 THEN videos.videos_id END DESC
LIMIT 11    

它返回以下结果

First Query Returns Image

如果我使用DESC或ASC使用第一个订单返回上面相同的结果。

我使用ASC使用第二个顺序返回结果:

Second Query Result image

我需要查询以下列顺序返回值

Result i need

请帮帮我

1 个答案:

答案 0 :(得分:2)

您的结果表明您想要:

order by total_views desc, videos_id desc

您可以在order by

中(安全地)引用列别名