mysql 作為常用數(shù)據(jù)庫,操作賊六是必須的,對于數(shù)字操作相關(guān)的東西,那是相當(dāng)方便,本節(jié)就來拎幾個統(tǒng)計案例出來供參考!
order訂單表,樣例如下:
CREATE TABLE `yyd_order` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`order_nid` varchar(50) NOT NULL,
`status` varchar(50) NOT NULL DEFAULT '0',
`money` decimal(20,2) NOT NULL DEFAULT '0.00',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `userid` (`user_id`),
KEY `createtime` (`create_time`),
KEY `updatetime` (`update_time`)
) ENGINE=InnoDB;
1. 按天統(tǒng)計進(jìn)單量,date_format
SELECT DATE_FORMAT(t.`create_time`, '%Y-%m-%d') t_date, COUNT(1) t_count FROM t_order t WHERE t.`create_time` > '2018-05-11' GROUP BY DATE_FORMAT(t.`create_time`, '%Y-%m-%d');
2. 按小時統(tǒng)計進(jìn)單量
SELECT DATE_FORMAT(t.`create_time`, '%Y-%m-%d %H') t_hour, COUNT(1) t_count FROM t_order t WHERE t.`create_time` > '2018-05-11' GROUP BY DATE_FORMAT(t.`create_time`, '%Y-%m-%d %H');
3. 同比昨天進(jìn)單量對比,order by h, date
SELECT DATE_FORMAT(t.`create_time`, '%Y-%m-%d %H') t_date, COUNT(1) t_count FROM yyd_order t WHERE t.`create_time` > '2018-05-11' GROUP BY DATE_FORMAT(t.`create_time`, '%Y-%m-%d %H')
ORDER BY DATE_FORMAT(t.`create_time`, '%H'),DATE_FORMAT(t.`create_time`, '%Y-%m-%d %H');
4. 環(huán)比上周同小時進(jìn)單,date in ,order by
SELECT DATE_FORMAT(t.`create_time`, '%Y-%m-%d %H') t_date, COUNT(1) t_count FROM yyd_order t WHERE
DATE_FORMAT(t.`create_time`,'%Y-%m-%d') IN ('2018-05-03','2018-05-11') GROUP BY DATE_FORMAT(t.`create_time`, '%Y-%m-%d %H')
ORDER BY DATE_FORMAT(t.`create_time`, '%H'),DATE_FORMAT(t.`create_time`, '%Y-%m-%d %H');
5. 按照remark字段中的返回值進(jìn)行統(tǒng)計,group by remark like ...
SELECT DATE_FORMAT(t.`create_time`, '%Y-%m-%d') t_date, COUNT(1) t_count, SUBSTRING_INDEX(SUBSTRING_INDEX(t.`msg`, '{', -1), '}', 1) t_rsp_msg FROM
cmoo_tab t WHERE t.`create_time` > '2018-05-17' AND t.`rsp_msg` LIKE '%nextProcessCode%C9000%'
GROUP BY DATE_FORMAT(t.`create_time`, '%Y-%m-%d'),SUBSTRING_INDEX(SUBSTRING_INDEX(t.`rsp_msg`, '{', -1), '}', 1);
6. 統(tǒng)計每小時的各金額的區(qū)間數(shù)統(tǒng)計,sum if 1 0,各自統(tǒng)計
SELECT DATE_FORMAT(t.create_time,'%Y-%m-%d') t_date, SUM(IF(t.`amount`>0 AND t.`amount`1000, 1, 0)) t_0_1000, SUM(IF(t.`amount`>1000 AND t.`amount`5000, 1, 0)) t_1_5000,
SUM(IF(t.`amount`>5000, 1, 0)) t_5000m FROM mobp2p.`yyd_order` t WHERE t.`create_time` > '2018-05-11' GROUP BY DATE_FORMAT(t.`create_time`, '%Y-%m-%d');
7. 按半小時統(tǒng)計進(jìn)單量,floor h / 30,同理10分鐘,20分鐘
SELECT CONCAT(DATE_FORMAT(create_time, '%Y-%m-%d %H:' ),IF(FLOOR(DATE_FORMAT(create_time, '%i') / 30 ) = 0, '00','30')) AS time_scope, COUNT(*)
FROM yyd_order WHERE create_time>'2018-05-11' GROUP BY time_scope ORDER BY DATE_FORMAT(create_time, '%H:%i'), DATE_FORMAT(create_time, '%Y-%m-%d') DESC ;
8. 成功率,失敗率,臨時表 join on hour
SELECT * FROM
(SELECT DATE_FORMAT(t.`create_time`, '%Y-%m-%d') t_date,COUNT(1) '成功數(shù)' FROM yyd_order t WHERE t.`create_time` > '2018-05-17' AND t.`status` = 'repay_yes' GROUP BY DATE_FORMAT(t.`create_time`, '%Y-%m-%d')) t1
RIGHT JOIN
(SELECT DATE_FORMAT(t.`create_time`, '%Y-%m-%d') t_date,COUNT(1) '總數(shù)' FROM yyd_order t WHERE t.`create_time` > '2018-05-11' GROUP BY DATE_FORMAT(t.`create_time`, '%Y-%m-%d')) t2 ON t1.t_date=t2.t_date;
9. 更新日志表中最后條一條日志狀態(tài)值到信息表中狀態(tài),update a join b on xx set a.status=b.status where tmp group by userid tmp2,注意索引
UPDATE t_order t0 LEFT JOIN (SELECT * FROM (SELECT * FROM t_order_log t WHERE t.create_time>'2018-05-11' ORDER BY id DESC) t1
GROUP BY t1.user_id ) ON t.user_id=t2.user_id SET t0.`status`=t2.status WHERE t0.`create_time`>'2018-05-11' AND t0.`status`=10;
10. 備份表,create table as select xxx where xxx
CREATE TABLE t_m AS SELECT * FROM t_order;
11. 純改備注不鎖表,快,類型全一致
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
您可能感興趣的文章:- PHP+MySQL實(shí)現(xiàn)對一段時間內(nèi)每天數(shù)據(jù)統(tǒng)計優(yōu)化操作實(shí)例
- php 廣告點(diǎn)擊統(tǒng)計代碼(php+mysql)
- 概述MySQL統(tǒng)計信息
- mysql按照天統(tǒng)計報表當(dāng)天沒有數(shù)據(jù)填0的實(shí)現(xiàn)代碼
- MySQL按時間統(tǒng)計數(shù)據(jù)的方法總結(jié)