【问题标题】:SQL Top 2 Records/ValuesSQL Top 2 记录/值
【发布时间】:2013-08-28 01:39:10
【问题描述】:

您好,我有以下查询,它返回开始日期表中下一个开始日期的数据,我要做的是提取接下来 2 个开始日期的数据。任何人都可以帮助我吗?我已编辑以显示我遇到问题的特定部分,而不是提供整个查询,请记住这是一个子查询。

(a1.expstartdate = (select min(startdate) 
from cstreprts.dbo.startdates
where startdate+15 > @asofdate and sycampusid = a1.sycampusid) 
    or a1.startdate = (select min(startdate)
 from cstreprts.dbo.startdates where startdate+15 > @asofdate 
 and sycampusid = a1.sycampusid))

【问题讨论】:

  • 您应该尝试将您的查询简化为它的最小表达式(至少对于问题而言)。

标签: mysql sql max min


【解决方案1】:

您可以LIMIT您收到的金额。如果您 ORDER BY 递减值,LIMIT 只会返回您的前 2 个结果。

只需ORDER BY 然后LIMIT 2

一个简短的例子

SELECT start_date 
FROM start_date_table 
ORDER BY start_date DESC 
LIMIT 2;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    相关资源
    最近更新 更多