假设我有一个日期字段,我想计算
之间的天数日期字段上的选定日期和系统日期中的now()如何使用SQL进行查询?
答案 0 :(得分:3)
select DATEDIFF(columnwithdatename, NOW()) from table
答案 1 :(得分:1)
使用DateDiff功能:
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_datediff
e.g。
var timeIn = new DateTime(2016, 7, 25, 14, 0, 0);
var timeOut = new DateTime(2016, 7, 26, 5, 0, 0);
if ((timeOut.Date - timeIn.Date).TotalDays >= 1)
{
var hrs12to4am = Enumerable.Range(0, (int)(timeOut - timeIn).TotalHours + 1)
.Select(i => timeIn.AddHours(i)).Where(a => a.Hour < 4 && a.Date > timeIn).ToList();
var hrsOverTen = Enumerable.Range(0, (int)(timeOut - timeIn).TotalHours + 1)
.Select(i => timeIn.AddHours(i)).Where(a => a.Hour > 22).ToList();
}
else
{
var hrsOverTen = Enumerable.Range(0, (int)(timeOut - timeIn).TotalHours + 1)
.Select(i => timeIn.AddHours(i)).Where(a => a.Hour > 22).ToList();
}
答案 2 :(得分:0)
使用sql datediff函数 例如
select datediff(day, myDate, getdate()) as daysAgo
from myTable
答案 3 :(得分:0)
也许尝试使用DATEDIFF
SQL函数(http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff)。
例如:
SELECT DATEDIFF(NOW(), your_date_field) FROM your_table;