我需要那个查询
分组类别(类别限制5)的最新消息和published_at的排序。有人帮我吗?
表
消息
1. id
2. caption
3. published_at
类别
1. id
2. name
news_has_category
1. news_id
2. category_id
示例输出
caption01.. 2016-01-28 category_id = 4
caption02.. 2016-01-27 category_id = 4
caption03.. 2016-01-26 category_id = 4
caption04.. 2016-01-25 category_id = 4
caption05.. 2016-01-24 category_id = 4
caption06.. 2016-01-28 category_id = 3
caption07.. 2016-01-27 category_id = 3
caption08.. 2016-01-26 category_id = 3
caption09.. 2016-01-25 category_id = 3
caption10.. 2016-01-24 category_id = 3
caption11.. 2016-01-28 category_id = 6
caption12.. 2016-01-27 category_id = 6
caption13.. 2016-01-26 category_id = 6
caption14.. 2016-01-25 category_id = 6
caption15.. 2016-01-24 category_id = 6
答案 0 :(得分:0)
我尝试根据你的表结构在mysql中创建表。 首先,您需要按类别进行分组,然后按分组类别按日期进行分组。
select caption , published_at,category_id from news,news_has_cat where id = news_id order by category_id desc,published_at;
上面的查询给出了在我的情况下按类别和日期按升序排序的输出反向。因此,我必须执行以下查询以满足您的要求。
select caption , published_at,category_id from news,news_has_cat where id = news_id order by category_id desc,published_at desc;
试一试