mysql日期相关查询总结

面试的时候,面试官不可能只问你最基本的增删改查,你还需要学习稍微复杂一些的查询,由于没有去过公司实习,故感觉自己对这些查询的熟练度完全不够,特此总结一些常用查询方法。

创建数据表

DROP TABLE IF EXISTS `good_order`;
CREATE TABLE `good_order`  (
  `bid` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单ID',
  `vid` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'vipID',
  `good_num` int(11) NULL DEFAULT NULL COMMENT '货品数量',
  `money` double(11, 2) NULL DEFAULT NULL COMMENT '订单金额',
  `fmoney` double(11, 2) NULL DEFAULT NULL COMMENT '最终支付金额',
  `created_time` datetime(3) NULL DEFAULT NULL COMMENT '订单创建时间',
  `good_id` int(255) NULL DEFAULT NULL COMMENT '商品ID',
  `commodity_specification` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品属性',
  `order_status` int(255) NULL DEFAULT NULL COMMENT '订单状态',
  PRIMARY KEY (`bid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 723 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

添加数据

INSERT INTO `good_order` VALUES (704, '123456', 1, 50.00, 7617.00, '2020-02-12 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (705, '123456', 1, 50.00, 98614.00, '2020-02-13 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (706, '123456', 1, 50.00, 86518.00, '2020-02-14 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (707, '123456', 1, 50.00, 6246.00, '2020-02-15 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (708, '123456', 1, 50.00, 5782.00, '2020-02-16 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (709, '123456', 1, 50.00, 18102.00, '2020-02-17 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (710, '123456', 1, 50.00, 6613.00, '2020-02-18 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (711, '123456', 1, 50.00, 10857.00, '2020-02-19 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (712, '123456', 1, 50.00, 59046.00, '2020-02-20 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (713, '123456', 1, 50.00, 35589.00, '2020-02-21 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (714, '123456', 1, 50.00, 20275.00, '2020-02-22 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (715, '123456', 1, 50.00, 30643.00, '2020-02-23 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (716, '123456', 1, 50.00, 58552.00, '2020-02-24 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (717, '123456', 1, 50.00, 6226.00, '2020-03-03 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (718, '123456', 1, 50.00, 32865.00, '2020-03-03 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (719, '123456', 1, 50.00, 64380.00, '2020-03-03 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (720, '123456', 1, 50.00, 32503.00, '2020-03-03 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (721, '123456', 1, 50.00, 79259.00, '2020-03-02 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (722, '123456', 1, 50.00, 97098.00, '2020-03-02 12:00:00.000', 42, 'middleCup#littleSweet', 1);
INSERT INTO `good_order` VALUES (723, '123456', 1, 50.00, 8633.00, '2020-03-02 12:00:00.000', 42, 'middleCup#littleSweet', 0);

找出数据表中交易量最多的一天

先 按照年月日分组,查找到某天有最多的交易量,之后再去分组,如果某组的交易条数为最大值,则输出该组的日期值。

SELECT
	date(created_time) 时间 ,count( * ) 交易笔数
FROM
	good_order 
GROUP BY
	DATE_FORMAT( created_time, '%Y-%m-%d' ) 
HAVING
	count( * ) = (
	SELECT
		max( mycount ) 
	FROM
	( SELECT *, count( * ) mycount FROM `good_order` GROUP BY DATE_FORMAT( created_time, '%Y-%m-%d' ) ) AS total 
	);

查询结果:
mysql日期相关查询总结_第1张图片

找出指定年月份2020-3月份中交易量最多的天数

SELECT
	date( created_time ),
	count( * ) 
FROM
	good_order 
WHERE
	DATE_FORMAT( created_time, '%Y-%m' ) = '2020-03' 
GROUP BY
	DAY ( created_time ) 
HAVING
	count( * ) = (
	SELECT
		max( mycount ) 
	FROM
	( SELECT *, count( * ) mycount FROM `good_order` WHERE DATE_FORMAT( created_time, '%Y-%m' ) = '2020-03' GROUP BY DAY ( created_time ) ) AS total 
	);

找出某月每日交易总笔数以及交易成功的交易额

先按照年月日分组,算出每日交易总笔数,然后找出交易成功的条件算出交易额

SELECT
	count( * ) 交易总笔数,
	(
	SELECT
		sum( money ) 
	FROM
		good_order a 
	WHERE
		DATEDIFF( a.created_time, b.created_time ) = 0 
		AND a.order_status = 1 
	) 交易成功总额,
	date(created_time) 日期
FROM
	good_order b 
GROUP BY
	DATE_FORMAT( created_time, '%Y-%m-%d' ) 
ORDER BY
	sum( money ) DESC;

查询结果:
mysql日期相关查询总结_第2张图片

找出指定月份2020-3月份的交易总数以及交易成功的交易额

SELECT
	count( * ) 交易总笔数,
	(
	SELECT
		sum( money ) 
	FROM
		good_order a 
	WHERE
		DATEDIFF( a.created_time, b.created_time ) = 0 
		AND a.order_status = 1 
	) 交易成功总额,
	date( created_time ) 日期 
FROM
	good_order b 
WHERE
	DATE_FORMAT( created_time, '%Y-%m' ) = '2020-03' 
GROUP BY
	DAY ( created_time ) 
ORDER BY
	sum( money ) DESC;

mysql 常用日期查询

#时期格式化
select  date_format(now() ,'%Y-%m-%d %T');
select date_format(now(),'%Y-%m-%d %H:%i:%s');
#查找前一个小时
select DATE_SUB(now(), interval 1 hour);
#查找前一个秒
select DATE_SUB(now(), interval 1 second);
#查找后30分钟
select date_add(now(),interval 30 minute);
#今天
select * from 表名 where to_days(时间字段名) = to_days(now());
SELECT * FROM 表名 WHERE ( datediff ( update_time , '2017-09-27' ) = 0 )
SELECT * FROM 表名 WHERE year(update_time ) = 2017 and month(update_time )= 09 and day(update_time ) = 27
#昨天
SELECT * FROM 表名 WHERE DATEDIFF(字段,NOW())=-1;
#本周
SELECT * FROM 表名 WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
#上周
SELECT * FROM 表名 WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
#近7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)
#近30天 
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)
#本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
#上一个月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1
#本季度
SELECT * FROM 表名 where QUARTER(create_date)=QUARTER(now());
#上季度 
SELECT * FROM 表名 where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#本年
SELECT * FROM 表名  where YEAR(create_date)=YEAR(NOW());
#去年
SELECT * FROM 表名 where year(create_date)=year(date_sub(now(),interval 1 year));

你可能感兴趣的:(Mysql,面试)