我有一个新闻列表,分为三个类别。假设category1,category2,category3。每篇文章都有发布日期。现在可以按以下方式获取列表:
most recent article in category1
most recent article in category2
most recent article in category3
second most recent article in category1
second most recent article in category2
second most recent article in category3
并且,如果category1和category3都剩下一篇文章,而category2用完了文章,则上面的列表后面将跟着:
third most recent article in category1
third most recent article in category3
我不知道这是否可能在mysql查询中。请帮忙!
最佳答案
如果您的类别编号受到限制,则可以循环它,并且在每个循环中,用UNION连接sql。这个SQL:"SELECT * FROM news WHERE 1 GROUP BY ".$category." ORDER BY publish_date DESC LIMIT ".$categoryCount." UNION ALL"
注意:不要忘记在最后一个“ UNION ALL”字符串的后面加上substr
关于php - 复杂的mysql查询-按日期分组和排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28960516/