当前位置:Gxlcms > 数据库问题 > 简单按日期查询mysql某张表中的记录数

简单按日期查询mysql某张表中的记录数

时间:2021-07-01 10:21:17 帮助过:7人阅读

测试表表结构:
mysql> show create table dr_stats\G
1. row

       Table: dr_stats
Create Table: CREATE TABLE `dr_stats` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `views` int(10) NOT NULL DEFAULT ‘0‘ COMMENT ‘展示量‘,
  `num` mediumint(8) NOT NULL DEFAULT ‘0‘ COMMENT ‘计费量‘,
  `advnum` int(10) NOT NULL DEFAULT ‘0‘ COMMENT ‘广告商计费数‘,
  `clicks` mediumint(8) NOT NULL DEFAULT ‘0‘ COMMENT ‘点击量‘,
  `do2click` mediumint(8) NOT NULL DEFAULT ‘0‘ COMMENT ‘二次点击量‘,
  `day` date NOT NULL DEFAULT ‘0000-00-00‘ COMMENT ‘计费日期‘,
  `planid` mediumint(8) NOT NULL DEFAULT ‘0‘ COMMENT ‘计划ID‘,
  `uid` mediumint(8) NOT NULL DEFAULT ‘0‘ COMMENT ‘站长ID‘,
  `siteid` mediumint(8) NOT NULL DEFAULT ‘0‘ COMMENT ‘站点ID‘,
  `zoneid` mediumint(8) NOT NULL DEFAULT ‘0‘ COMMENT ‘广告位ID‘,
  `adstypeid` mediumint(8) NOT NULL COMMENT ‘广告类型ID‘,
  `deduction` mediumint(8) NOT NULL DEFAULT ‘0‘ COMMENT ‘扣量‘,
  `sumprofit` decimal(10,4) NOT NULL DEFAULT ‘0.0000‘ COMMENT ‘扣量金额‘,
  `sumpay` decimal(10,4) NOT NULL DEFAULT ‘0.0000‘ COMMENT ‘计费金额‘,
  `sumadvpay` decimal(10,4) NOT NULL DEFAULT ‘0.0000‘ COMMENT ‘计费总金额‘,
  `status` tinyint(1) NOT NULL DEFAULT ‘0‘ COMMENT ‘是否已结算‘,
  `dosage` mediumint(8) NOT NULL DEFAULT ‘0‘ COMMENT ‘补量‘,
  `sumdosage` decimal(10,4) NOT NULL DEFAULT ‘0.0000‘ COMMENT ‘补量金额‘,
  `pclick` int(8) NOT NULL DEFAULT ‘0‘,
  PRIMARY KEY (`id`),
  UNIQUE KEY `day` (`day`,`planid`,`uid`,`siteid`,`zoneid`,`adstypeid`),
  KEY `planid_uid` (`planid`,`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=9298495886 DEFAULT CHARSET=utf8 COMMENT=‘站点结算‘
1 row in set (0.00 sec)

查询一张表中一年的中4月份的数据:

mysql -uroot -p‘ZykJ7‘ -S /tmp/mysql.sock -e ‘use drnew;select * from dr_stats where 1 and month(day)=04;‘

查询一张表中一年的中5月份有多少条记录:
[root@cacti ~]# time mysql -uroot -p‘ZykJ7‘ -S /tmp/mysql.sock -e ‘use drnew;select count(*) from dr_stats where 1 and month(day)=05;‘
Warning: Using a password on the command line interface can be insecure.
+----------+
| count(*) |
+----------+
|  1071903 |
+----------+

[root@cacti ~]# time mysql -uroot -p‘ZykJ7‘ -S /tmp/mysql.sock -e ‘use drnew;select count(*) from dr_stats where 1 and month(day)=05 and  day(day)  between 1 and 31;‘
Warning: Using a password on the command line interface can be insecure.
+----------+
| count(*) |
+----------+
|  1071903 |
+----------+

查询dr_stats表2016年 4月1日至2016年4月21日的数据记录数:


time mysql -uroot -p‘ZykJ7‘ -S /tmp/mysql.sock -e ‘use drnew;select count(*) from dr_stats where 1 and year(day)=2016 and month(day)=04 and  day(day)  between 1 and 21;‘ 
Warning: Using a password on the command line interface can be insecure.
+----------+
| count(*) |
+----------+
|        0 |
+----------+

2016年 4月1日至2016年4月21日这个dr_stats表是没有数据的

查询dr_stats表2016年 4月1日至2016年4月22日的数据记录数:

time mysql -uroot -p‘ZykJ(5678%$#@!)cpv17‘ -S /tmp/mysql.sock -e ‘use drnew;select count(*) from dr_stats where 1 and year(day)=2016 and month(day)=04 and  day(day)  between 1 and 22;‘ 
Warning: Using a password on the command line interface can be insecure.
+----------+
| count(*) |
+----------+
|       95 |
+----------+

查询dr_stats表2016年 4月22日至2016年4月23日的数据:
time mysql -uroot -p‘ZykJ7‘ -S /tmp/mysql.sock -e ‘use drnew;select * from dr_stats where 1 and year(day)=2016 and month(day)=04 and day(day) between 22 and 23;‘ >>/root/txt04

查询dr_stats表2016年 4月22日至2016年4月22日的数据:

[root@cacti www]# time mysql -uroot -p‘ZykJ7‘ -S /tmp/mysql.sock -e ‘use drnew;select count(*) from dr_stats where day>="2016-04-22" and day<="2016-04-22";‘
Warning: Using a password on the command line interface can be insecure.
+----------+
| count(*) |
+----------+
|       95 |
+----------+

查询dr_stats表2016年 4月21日至2016年4月22日的数据:


time mysql -uroot -p‘ZykJ7‘ -S /tmp/mysql.sock -e ‘use drnew;select count(*) from dr_stats where day>="2016-04-21" and day<="2016-04-22";‘
Warning: Using a password on the command line interface can be insecure.
+----------+
| count(*) |
+----------+
|       95 |
+----------
```+

简单按日期查询mysql某张表中的记录数

标签:UI   select   查询   typeid   cacti   测试   txt   status   word   

人气教程排行