mysql查询当天所有数据sql语句
mysql查詢當(dāng)天的所有信息:
select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())
這個(gè)有一些繁瑣,還有簡(jiǎn)單的寫法:
select * from table where date(regdate) = curdate();
另一種寫法沒測(cè)試過(guò)
查詢當(dāng)天的記錄
select * from hb_article_view where TO_DAYS(hb_AddTime) = TO_DAYS(NOW())
date()函數(shù)獲取日期部分, 扔掉時(shí)間部分,然后與當(dāng)前日期比較即可
補(bǔ)充:本周、上周、本月、上個(gè)月份的數(shù)據(jù)
查詢當(dāng)前這周的數(shù)據(jù)
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
查詢上周的數(shù)據(jù)
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
查詢當(dāng)前月份的數(shù)據(jù)
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
查詢距離當(dāng)前現(xiàn)在6個(gè)月的數(shù)據(jù)
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查詢上個(gè)月的數(shù)據(jù)
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')
select * from `user` where DATE_FORMAT(pudate,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ;
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now())
select *
from user
where MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())
select *
from [user]
where YEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = YEAR(now())
and MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())
select *
from [user]
where pudate between 上月最后一天
and 下月第一天
mysql查詢多少秒內(nèi)的數(shù)據(jù)
SELECT count( * ) AS c, sum( if( logusertype =2, logusertype, 0 ) ) /2 AS a, sum( if( logusertype =3, logusertype, 0 ) ) /3 AS b
FROM testlog WHERE UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP( logendtime )<=30
查詢30秒內(nèi)記錄的總數(shù),loguser等于2的記錄的總數(shù)和,和 loguser等于3的記錄的總數(shù).
if( logusertype =2, logusertype, 0 ) 如果logusetype等于2 就在logusertype上累加,否則加0。
sum( if( logusertype =2, logusertype, 0 ) ) 把logusertype都累加起來(lái)。
sum( if( logusertype =2, logusertype, 0 ) ) /2 AS a, 除以2是統(tǒng)計(jì)個(gè)數(shù)。
UNIX_TIMESTAMP(NOW())計(jì)算當(dāng)前時(shí)間的秒數(shù),
UNIX_TIMESTAMP( logendtime )計(jì)算logendtime的秒數(shù)
http://www.3lian.com/edu/2013/08-29/93024.html
?
DATE_FORMAT(date,format)
DATE_FORMAT(date,format)date?參數(shù)是合法的日期。format?規(guī)定日期/時(shí)間的輸出格式。
可以使用的格式有:
| %a | 縮寫星期名 |
| %b | 縮寫月名 |
| %c | 月,數(shù)值 |
| %D | 帶有英文前綴的月中的天 |
| %d | 月的天,數(shù)值(00-31) |
| %e | 月的天,數(shù)值(0-31) |
| %f | 微秒 |
| %H | 小時(shí) (00-23) |
| %h | 小時(shí) (01-12) |
| %I | 小時(shí) (01-12) |
| %i | 分鐘,數(shù)值(00-59) |
| %j | 年的天 (001-366) |
| %k | 小時(shí) (0-23) |
| %l | 小時(shí) (1-12) |
| %M | 月名 |
| %m | 月,數(shù)值(00-12) |
| %p | AM 或 PM |
| %r | 時(shí)間,12-小時(shí)(hh:mm:ss AM 或 PM) |
| %S | 秒(00-59) |
| %s | 秒(00-59) |
| %T | 時(shí)間, 24-小時(shí) (hh:mm:ss) |
| %U | 周 (00-53) 星期日是一周的第一天 |
| %u | 周 (00-53) 星期一是一周的第一天 |
| %V | 周 (01-53) 星期日是一周的第一天,與 %X 使用 |
| %v | 周 (01-53) 星期一是一周的第一天,與 %x 使用 |
| %W | 星期名 |
| %w | 周的天 (0=星期日, 6=星期六) |
| %X | 年,其中的星期日是周的第一天,4 位,與 %V 使用 |
| %x | 年,其中的星期一是周的第一天,4 位,與 %v 使用 |
| %Y | 年,4 位 |
| %y | 年,2 位 |
實(shí)例
下面的腳本使用 DATE_FORMAT() 函數(shù)來(lái)顯示不同的格式。我們使用 NOW() 來(lái)獲得當(dāng)前的日期/時(shí)間:
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p') DATE_FORMAT(NOW(),'%m-%d-%Y') DATE_FORMAT(NOW(),'%d %b %y') DATE_FORMAT(NOW(),'%d %b %Y %T:%f')結(jié)果類似:
Dec 29 2008 11:45 PM 12-29-2008 29 Dec 08 29 Dec 2008 16:25:46.635?
總結(jié)
以上是生活随笔為你收集整理的mysql查询当天所有数据sql语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java设定一个日期时间,加几分钟(小时
- 下一篇: 在centos6.5上安装mongodb