1. 程式人生 > >簡單按日期查詢mysql某張表中的記錄數

簡單按日期查詢mysql某張表中的記錄數

unsigned 計劃 表結構 記錄 create sig 站點 signed using

測試表表結構:
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某張表中的記錄數