首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MYSQL查询:最后N行+设置了特定标志的所有行

MYSQL查询:最后N行+设置了特定标志的所有行
EN

Stack Overflow用户
提问于 2016-05-25 13:56:56
回答 1查看 41关注 0票数 0

我需要在页面上显示从数据库中获取的事件列表。我查询的特殊性取决于在同一个表中存在不同类型的事件,然后是不同的选择条件/筛选器。

事件表的(简化)模式:

代码语言:javascript
复制
Id INT UNSIGNED AI PK
Type TINYINT
Important BOOL
DateStart DATE
DateStop DATE

在我的主页上有两种类型的事件:

  • :我只想要last 10 rows (按日期排序) where [Type=1],(DateStart和DateStop总是一样的)。
  • Actions:每一行where [Type=2] and [Show = true] and [DateStart >= NOW] and [DateStop <= NOW],结果都不能在新闻的10行限制中计算.

所有这些(10条新闻+显示的操作)都由DateStop ASC排序。

在一个查询中可以这样做吗?我曾想过联合,但我想知道是否有更简单的解决办法。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2016-05-25 14:19:35

为什么不联合呢?

代码语言:javascript
复制
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的语法可能会有所不同。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37439387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档