MySql如何按照日期進(jìn)行分組統(tǒng)計
最近接到一個需求,就是按照日期進(jìn)行統(tǒng)計數(shù)量,數(shù)據(jù)庫表里有id(編號),date(日期),phone(手機(jī)號)
數(shù)據(jù)庫
CREATE TABLE `tb_count` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '編號', `tiems` datetime(0) NOT NULL COMMENT '日期', `number` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '手機(jī)號', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
我的sql是這樣的
這一條sql是查詢所有的,可以按照這個方法進(jìn)行分頁查詢,查詢所有
SELECT COUNT(*) AS counts,DATE_FORMAT(tiems,"%Y年%m月%d日") AS dates FROM tb_count GROUP BY DATE_FORMAT (tiems,"%Y年%m月%d日") -- DATE_FORMAT();按照格式對某個日期操作, -- tb_count 表名 -- period 日期 -- 整段代碼的意思是:從tb_count表中 查詢?nèi)掌冢╬eriod )(將日期按照"%Y年%m月"格式)并作為dates,統(tǒng)計數(shù)量(count(*)),按照日期分組 GROUP BY DATE_FORMAT(period ,"%Y年%m月")
如果是進(jìn)行分頁查詢 我是做了一個封裝,將count(數(shù)量)和period(時間)做了一個封裝,做了一個實(shí)體類,技術(shù)有限,只能想到封裝為一個對象
封裝對象
@ApiModel(description = "Statis",value = "Statis")
public class Statis implements Serializable {
//數(shù)量
@ApiModelProperty(value = "",required = false)
private int counts;
//時間
@ApiModelProperty(value = "",required = false)
private String dates;
public int getCounts() {
return counts;
}
public void setCounts(int counts) {
this.counts = counts;
}
public String getDates() {
return dates;
}
public void setDates(String dates) {
this.dates = dates;
}
}Service層
/**
* 分頁查詢
* @param page 當(dāng)前頁
* @param size 每頁顯示的條數(shù)
* @return
*/
Result findPage(int page,int size);ServiceImpl Service層實(shí)現(xiàn)類
我這邊用分頁插件是PageInfo,需要傳入一個page和size
/**
* 分頁查詢
* @param page 當(dāng)前頁
* @param size 每頁顯示的條數(shù)
* @return
*/
@Override
public Result findPage(int page, int size) {
try {
//分頁查詢
PageHelper.startPage(page, size);
//查詢所有
List<Statis> statisList = countsMapper.findAll();
PageInfo<Statis> pageInfo = new PageInfo<Statis>(statisList);
return new Result(true,StatusCode.OK,"分頁查詢成功",pageInfo);
}catch (Exception e){
e.printStackTrace();
return new Result(false,StatusCode.ERROR,"分頁查詢失敗");
}
}Controller層
/**
* 分頁查詢
* @param page 當(dāng)前頁
* @param size 每頁顯示的條數(shù)
* @return
* @throws Exception
*/
@GetMapping("/search/{page}/{size}")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "path", name = "page", value = "當(dāng)前頁", required = true, dataType = "Integer"),
@ApiImplicitParam(paramType = "path", name = "size", value = "每頁顯示條數(shù)", required = true, dataType = "Integer")
})
@ApiOperation(value = "日期分頁查詢",notes = "日期分頁方法詳情",tags = {"MemberController"})
public Result findPage(@PathVariable int page,@PathVariable int size) throws Exception {
return countsService.findPage(page, size);
}最后就是測試了

這個需求不是很難,就是一個簡單的分頁查詢而已,難點(diǎn)在于這個sql。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- MySQL按天分組統(tǒng)計一定時間內(nèi)的數(shù)據(jù)實(shí)例(沒有數(shù)據(jù)補(bǔ)0)
- mysql如何分別按年/月/日/周分組統(tǒng)計數(shù)據(jù)詳解
- mysql如何分組統(tǒng)計并求出百分比
- mysql實(shí)現(xiàn)按照某個時間段分組統(tǒng)計
- Mysql出生日期轉(zhuǎn)換為年齡并分組統(tǒng)計人數(shù)的方法示例
- MYSQL每隔10分鐘進(jìn)行分組統(tǒng)計的實(shí)現(xiàn)方法
- mysql按天/小時/半小時/N分鐘/分鐘進(jìn)行數(shù)據(jù)分組統(tǒng)計功能
相關(guān)文章
微信昵稱帶符號導(dǎo)致插入MySQL數(shù)據(jù)庫時出錯的解決方案
Mysql的utf8編碼最多3個字節(jié),而Emoji表情或者某些特殊字符是4個字節(jié),所以會導(dǎo)致帶有表情的昵稱插入數(shù)據(jù)庫時出錯,下面給大家分享下解決方案,需要的朋友參考下吧2016-12-12
MySQL thread_stack連接線程的優(yōu)化
當(dāng)有新的連接請求時,MySQL首先會檢查Thread Cache中是否存在空閑連接線程,如果存在則取出來直接使用,如果沒有空閑連接線程,才創(chuàng)建新的連接線程2017-04-04

