如何从某个日期开始显示

时间:2018-01-12 04:29:56

标签: sql database

我希望显示某个日期之后的日期,但将其限制为7行 例如:我想显示2018-01-11和最多7行的日期

SELECT 
EmployeeID,[Date], ShiftType, Shift_Description
FROM
Roster r inner join ShiftType s on r.ShiftType = s.ShiftID 
WHERE EmployeeID = 123123
ORDER BY [Date] ASC;

Result

3 个答案:

答案 0 :(得分:2)

您可以为查询添加日期条件和限制:

SELECT 
EmployeeID,[Date], ShiftType, Shift_Description
FROM
Roster r inner join ShiftType s on r.ShiftType = s.ShiftID 
WHERE EmployeeID = 123123 AND Date > '2018-01-11'
ORDER BY [Date] ASC
LIMIT 7;

答案 1 :(得分:2)

MySQL使用Limit

SELECT 
EmployeeID,[Date], ShiftType, Shift_Description
FROM
Roster r inner join ShiftType s on r.ShiftType = s.ShiftID 
WHERE EmployeeID = 123123 AND Date >= '2018-01-11'
ORDER BY [Date] ASC
LIMIT 7;

SQL Server使用TOP

SELECT Top 7
EmployeeID,[Date], ShiftType, Shift_Description
FROM
Roster r inner join ShiftType s on r.ShiftType = s.ShiftID 
WHERE EmployeeID = 123123 AND Date >= '2018-01-11'
ORDER BY [Date] ASC;

答案 2 :(得分:2)

您没有指定数据库,但它似乎是屏幕截图和您的查询语法中的SQL Server,因此我将基于此回答。

添加日期条件后,您可以使用TOP语句获取前7条记录:

SELECT TOP 7
EmployeeID, [Date], ShiftType, Shift_Description
FROM Roster r
INNER JOIN ShiftType s ON r.ShiftType = s.ShiftID 
WHERE EmployeeID = 123123
  AND [DATE] >= '2018-01-11'
ORDER BY [Date] ASC;

您还可以使用DATEDIFF()功能比较日期。在此查询中,它始于2018-01-10之后的至少一天:

SELECT TOP 7
EmployeeID, [Date], ShiftType, Shift_Description
FROM Roster r
INNER JOIN ShiftType s ON r.ShiftType = s.ShiftID 
WHERE EmployeeID = 123123
  AND DATEDIFF(DAY, '2018-01-10', [DATE]) > 0
ORDER BY [Date] ASC;