Sql 查找两个日期之间的记录&如果未找到,则返回具有最小日期的记录

Sql 查找两个日期之间的记录&如果未找到,则返回具有最小日期的记录,sql,Sql,我有一张像这样的桌子 Name DateOfBirth -------------------------------- Ramesh 17/04/1983 Pavan 16/03/1980 Rakesh 18/03/1975 Nadeem 19/05/1974 Kalam 20/08/2973 我正在编写一个SP,其伪代码如下所示: 我正在将日期作为输入参数传递到此SP 如果找到@InputDate,SP应返回记录 如果@In

我有一张像这样的桌子

Name       DateOfBirth
--------------------------------
Ramesh     17/04/1983

Pavan      16/03/1980

Rakesh     18/03/1975

Nadeem     19/05/1974

Kalam      20/08/2973 
我正在编写一个SP,其伪代码如下所示:

我正在将日期作为输入参数传递到此SP

如果找到@InputDate,SP应返回记录 如果@InputDate小于表中的最小日期,则应返回具有最小日期的记录 如果未找到@InputDate,但它位于列中的最小值和最大值之间,则应返回稍后将立即落下的记录

可以在一条语句中编写逻辑吗? 完成此任务的最佳方式
我认为MySQL中的以下查询提供了您想要的:

SELECT Name, DateOfBirth
FROM mytable
WHERE DateOfBirth >= @mydate
ORDER BY DateOfBirth LIMIT 1
案例1,输入:

输出:

Name,   DateOfBirth
-------------------
Nadeem, 1974-05-19
Name,   DateOfBirth
-------------------
Pavan,  1980-03-16
Name,    DateOfBirth
-------------------
Ramesh,  1983-04-17
Name,    DateOfBirth
-------------------
No records returned
案例2,输入:

输出:

Name,   DateOfBirth
-------------------
Nadeem, 1974-05-19
Name,   DateOfBirth
-------------------
Pavan,  1980-03-16
Name,    DateOfBirth
-------------------
Ramesh,  1983-04-17
Name,    DateOfBirth
-------------------
No records returned
案例3,输入:

输出:

Name,   DateOfBirth
-------------------
Nadeem, 1974-05-19
Name,   DateOfBirth
-------------------
Pavan,  1980-03-16
Name,    DateOfBirth
-------------------
Ramesh,  1983-04-17
Name,    DateOfBirth
-------------------
No records returned
案例4,输入:

输出:

Name,   DateOfBirth
-------------------
Nadeem, 1974-05-19
Name,   DateOfBirth
-------------------
Pavan,  1980-03-16
Name,    DateOfBirth
-------------------
Ramesh,  1983-04-17
Name,    DateOfBirth
-------------------
No records returned

是的,你可以。首先,让我们看看如何获得具有集合的最小和最大日期的记录:

最小值:

最大值:

以下是获取匹配记录的方式:

现在,让我们将所有内容合并到一个查询中:

select mymin.Name as myminName, mymin.DateOfBirth as myminDateOfBirth,
       mymax.Name as mymaxName, myMax.DateOfBirth as mymaxDateOfBirth,
       therecord.Name as therecordName, therecord.DateOfBirth as therecordDateOfBirth
from
(select top 1 Name, DateOfBirth
from yourtable
order by DateOfBirth) mymin
join
(select top 1 Name, DateOfBirth
from yourtable
order by DateOfBirth desc) mymax
on 1 = 1
left join yourtable therecord
on therecord.DateOfBirth = @InputDate
如您所见,我们可以选择所有可能的值。最后一步是修改选择以仅获取所需记录的名称和出生日期。如果不匹配且日期不小于最小值且不大于最大值,则将返回空值。为此,我们需要在语法时使用case,如下所示:

select case (therecord.Name is null)
When 1 then (case mymin.DateOfBirth > @InputDate when 1 then mymin.Name
             else case mymax.DateOfBirth < @InputDate when 1 then mymax.Name else null end)
Else therecord.Name
End as Name,
case (therecord.Name is null)
When 1 then (case mymin.DateOfBirth > @InputDate when 1 then mymin.DateOfBirth
             else case mymax.DateOfBirth < @InputDate when 1 then mymax.DateOfBirth else null end)
Else therecord.DateOfBirth
End as DateOfBirth
from
(select top 1 Name, DateOfBirth
from yourtable
order by DateOfBirth) mymin
join
(select top 1 Name, DateOfBirth
from yourtable
order by DateOfBirth desc) mymax
on 1 = 1
left join yourtable therecord
on therecord.DateOfBirth = @InputDate
我假设您正在使用SQL Server


警告:所有代码都没有经过测试,如果有任何拼写错误,请告诉我。

您使用的是哪种RDBMS?任何数据库都将在最新/最新版本时停止使用,因为许多DBM远远不符合ANSI SQL…很好-这可以很容易地转换为SQL的其他变体。