在數(shù)據(jù)庫的使用中,經(jīng)常需要按指定日期來查詢記錄,以便于統(tǒng)計,而在數(shù)據(jù)庫中,有很多存儲的是時間戳,
也有的直接存日期,查詢的時候可能不是那么好弄.
mysql提供了兩個函數(shù):
from_unixtime(time_stamp) -> 將時間戳轉(zhuǎn)換為日期
unix_timestamp(date) -> 將指定的日期或者日期字符串轉(zhuǎn)換為時間戳
[plain]
select from_unixtime(1382544000);
+---------------------------+
| from_unixtime(1382544000) |
+---------------------------+
| 2013-10-24 00:00:00 |
+---------------------------+
如: unix_timestamp(date)
[plain]
select unix_timestamp(date('2013-10-24'));
+------------------------------------+
| unix_timestamp(date('2013-10-24')) |
+------------------------------------+
| 1382544000 |
+------------------------------------+
如果要查詢當天的訂單的記錄:
[plain]
select count(*) from b_order Where date_format(from_unixtime(create_time),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')
也可以這樣:
[plain]
select count(*) from b_order Where create_time >= unix_timestamp('2013-10-24 00:00:00') and create_time <= unix_timestamp('2013-10-24 23:59:59') ;