我需要在页面上显示从数据库中获取的事件列表。我查询的特殊性取决于在同一个表中存在不同类型的事件,然后是不同的选择条件/筛选器。
事件表的(简化)模式:
Id INT UNSIGNED AI PK
Type TINYINT
Important BOOL
DateStart DATE
DateStop DATE在我的主页上有两种类型的事件:
last 10 rows (按日期排序) where [Type=1],(DateStart和DateStop总是一样的)。where [Type=2] and [Show = true] and [DateStart >= NOW] and [DateStop <= NOW],结果都不能在新闻的10行限制中计算.所有这些(10条新闻+显示的操作)都由DateStop ASC排序。
在一个查询中可以这样做吗?我曾想过联合,但我想知道是否有更简单的解决办法。谢谢!
发布于 2016-05-25 14:19:35
为什么不联合呢?
select tmp.type, tmp.important,tmp.datestart,tmp.datestop
from -- the 10 last news:
(select top 10 type, important,datestart,datestop
from yourtable
where type=1
order by id desc
union
select type, important,datestart,datestop
from yourtable
where type <>1) tmp -- in this where clause, add all the other conditions!
order by datestart asc -- or whichever date you want to order by您可能需要编辑它一点,因为我已经习惯了microsoft的sql服务器,因此mysql的语法可能会有所不同。
https://stackoverflow.com/questions/37439387
复制相似问题