这是我的表格项目
:
id type important date
--------------------------------------------------
1 articles 0 2013-11-14 01:10:00
2 articles 0 2013-11-14 02:45:00
3 articles 1 2013-11-15 03:00:00
4 articles 0 2013-11-15 04:23:00
5 articles 1 2013-11-17 05:21:00
6 news 0 2013-11-18 06:00:00
7 news 0 2013-11-19 07:00:00
8 news 0 2013-11-19 08:00:00
我需要显示三个重要的项目,或者,如果没有足够的项目,则将新闻添加到组合中,直到出现三个。然后按日期从最新到最旧对它们进行排序。
如果我简单地写成:
SELECT * FROM `items` WHERE important = 1 OR type = 'news'
ORDER BY `important` DESC, `date` DESC LIMIT 3
那么有两个重要的项目将高于最新的新闻项目。但我需要得到这个:
id type important date
--------------------------------------------------
8 news 0 2013-11-19 08:00:00
5 articles 1 2013-11-17 05:21:00
3 articles 1 2013-11-15 03:00:00
如何使用 SQL 执行此操作?
最佳答案
这样可以解决问题吗?
SELECT * FROM (
SELECT * FROM `items` WHERE important = 1 OR type = 'news'
ORDER BY `important` DESC, `date` DESC LIMIT 3
) x
ORDER BY `date` DESC
关于mysql - 选择一些项目,如果不够,则添加一些其他项目,然后排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19981150/