Mysql 查询
Posted 橘子味儿的猫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql 查询相关的知识,希望对你有一定的参考价值。
按时间 【年、月、周、日】 分组查询统计数据
<!-- 按日查询--> SELECT DATE_FORMAT(created_at,\'%Y-%m-%d\') as day, SUM(COUNT) FROM collect GROUP BY day; <!-- 按周查询--> SELECT DATE_FORMAT(created_at,\'%Y-%u\') as day, SUM(COUNT) FROM collect GROUP BY day; <!-- 按月查询--> SELECT DATE_FORMAT(created_at,\'%Y-%m\') as day, SUM(COUNT) FROM collect GROUP BY day; <!-- 按年查询--> SELECT DATE_FORMAT(created_at,\'%Y\') as day, SUM(COUNT) FROM collect GROUP BY day;
时间比较
- 存储格式:2018-12-19 17:00:32
SELECT * FROM t_bill where Date(bil_create_time) > \'2018-12-19 16:56:15\';
添加事件
- 每隔一分钟执行一次
CREATE event IF NOT EXISTS temp_event ON SCHEDULE EVERY 1 MINUTE ON COMPLETION PRESERVE DO update t_withdraw set wit_state=1 where wit_id=1;
- 每天凌晨1点执行
CREATE event IF NOT EXISTS temp_event ON SCHEDULE EVERY 1 DAY STARTS DATE_ADD(DATE_ADD(CURDATE(), INTERVAL 1 DAY), INTERVAL 1 HOUR) ON COMPLETION PRESERVE DO INSERT INTO T_LOG(date) VALUE (NOW())
- 查看定时任务
SELECT * FROM information_schema.events;
- 关闭定时任务
DROP event temp_event;
- 查看并打开event_scheduler调度事件
show variables like \'%sche%\';
注:如果event_scheduler值为OFF则使用 【set global event_scheduler =1;】 打开。
详情介绍:https://blog.csdn.net/tantexian/article/details/50317829
循环插入数据
- 循环插入 10000 条数据
DROP PROCEDURE IF EXISTS proc_initData; -- 如果存在此存储过程则删除 DELIMITER $ CREATE PROCEDURE proc_initData() BEGIN DECLARE i INT DEFAULT 1; WHILE i<=10000 DO INSERT INTO T_LOG(date,msg) VALUE (NOW(), i); SET i = i+1; END WHILE; END $ CALL proc_initData(); -- 调用方法
将查询结果插入到另外一张表
- 基本语法 INSERT INTO 表名1(字段列表1) SELECT 字段列表2 FROM 表名2 WHERE 查询条件;
INSERT INTO t_log(date,msg) SELECT now(), sho_id FROM t_shop
日期加一天
SELECT * FROM t_bill WHERE bil_create_time >= \'2018-12-20 00:00:00\' AND bil_create_time <= DATE_ADD(\'2018-12-20 00:00:00\',INTERVAL 1 DAY)
nodejs 获取 明天的日期
引入 【moment】
bower install moment --save # bower npm install moment --save # npm Install-Package Moment.js # NuGet spm install moment --save # spm meteor add momentjs:moment # meteor
调用
var moment = require(\'moment\'); // 在当前日期的基础上加24小时 --- 获取第二天的日期 var day = moment(body.date); var toDate = day.add(\'hours\',24).format(\'YYYY-MM-DD HH:mm:ss\');
嵌套查询
SELECT *, (SELECT COUNT(1) FROM t_shop WHERE sho_age_id = age_id) as c FROM t_agent
以上是关于Mysql 查询的主要内容,如果未能解决你的问题,请参考以下文章