使用DESC和ASC与Group By的顺序

时间:2010-08-26 07:37:19

标签: mysql

这是我的sql语句

 SELECT DISTINCT article_categories.category_id, 
   article_id,article_title,article_intro,article_content,
   category_name_ar 
 FROM articles,article_categories 
 WHERE articles.category_id=article_categories.category_id 
   AND article_type='admin' 
   AND category_case='active' 
 GROUP BY article_categories.category_id 
 ORDER BY article_categories.category_id ASC,
          article_date,article_time DESC 
 LIMIT 10

我想检索第一个DISTINCT 10类别。另外,我想检索articles.category_id等于article_categories.category_id的等效文章。到目前为止结果还不错,但我要找的是按category_id排序结果,同时按照article_date,article_time DESC对结果进行排序。

任何帮助都将提前得到赞赏

3 个答案:

答案 0 :(得分:3)

SELECT distinct c.category_id,...  
FROM articles a,article_categories c
WHERE a.category_id=c.category_id   
AND ...  
AND a.article_id in   
(SELECT max(a1.article_id)   
 FROM articles a1
 WHERE a1.category_id=c.category_id )
ORDER BY c.category_id ASC 

请用您需要的其他字段和条件替换....

答案 1 :(得分:1)

我明白了

SELECT DISTINCT article_categories.category_id, 
   article_id,article_title,article_intro,article_content,
   category_name_ar 
 FROM articles,article_categories 
 WHERE articles.category_id=article_categories.category_id 
   AND article_type='admin' 
   AND category_case='active' 
 ORDER BY article_categories.category_id ASC,
          article_date desc,article_time DESC 
 LIMIT 10

感谢您的所有建议,这有很多帮助

答案 2 :(得分:0)

仅添加 desc ,如

ORDER BY article_categories.category_id ASC,
          article_date DESC,article_time DESC