SQL 查詢某字段值在當天時間段內

2021-06-06 03:24:47 字數 592 閱讀 4843

問題:

乙個使用者在自然天一天只能投票一次。

需要查詢出當前使用者的投票時間是否在當天。

解決方法有:

1.  將unix時間戳轉換成date格式進行比較

select

from_unixtime(`time`) as feedtime, bid, uid, `type`

from feeds

where date_format(from_unixtime(`time`),'%y-%m-%d') = date_format(now(), '%y-%m-%d')

order by `time` desc

2. 將當天時間轉換成unix時間戳進行比較

select *  from feeds where `time` > unix_timestamp(curdate()) and `time` < unix_timestamp(curdate())+86400 order by `time`  desc

相比較,第二種方法的效率會更高一些。

因為time如果不是索引,會消耗運算的效率。

動態取當天時間段內的條件

最近專案裡需要以每天當天的日期為條件來查詢,想來想去覺得用datediff 函式可以實現,如下 select top 1 flag1 flag from messagegs where serviceid rcgs and and datediff dd,datetime,getdate 0 ord...

SQL時間段查詢

access的話 select from table where date1 2008 1 20 and date2 2007 8 30 mysql的話 select from table where date1 2008 1 20 and date2 2007 3 30 也可以 select fr...

SQL時間段查詢

sql時間段查詢 access的話 select from table where date1 2008 1 20 and date2 2007 8 30 mysql的話 select from table where date1 2008 1 20 and date2 2007 3 30 也可以 ...