MySQL grouped by date and count the total number of tutorial examples of the current cut-off time

MySQL grouped by date and count the total number of the current cut-off time

Construction of the table statement

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_reg
-- ----------------------------
DROP TABLE IF EXISTS `t_reg`;
CREATE TABLE `t_reg`  (
  `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID编号',
  `REG_TIME` datetime(0) NULL DEFAULT NULL COMMENT '时间',
  `REG_COUNT` int(11) NULL DEFAULT NULL COMMENT '数量',
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of t_reg
-- ----------------------------
INSERT INTO `t_reg` VALUES (1, '2019-05-01 08:08:12', 1);
INSERT INTO `t_reg` VALUES (2, '2019-05-02 00:09:09', 10);
INSERT INTO `t_reg` VALUES (3, '2019-05-02 13:08:08', 2);
INSERT INTO `t_reg` VALUES (4, '2019-05-03 15:08:05', 4);
INSERT INTO `t_reg` VALUES (5, '2019-05-03 17:08:08', 6);
INSERT INTO `t_reg` VALUES (6, '2019-05-16 21:01:12', 4);
INSERT INTO `t_reg` VALUES (7, '2019-05-03 05:08:09', 11);

SET FOREIGN_KEY_CHECKS = 1;

Table structure is as follows: REG_COUNT represents the day the new user

Now demand is this: a packet per day, the total number of user queries that day and the total number of new users to the current cut-off time new results are as follows:

SQL statement is as follows:

SELECT
    reg_time,
    min_total AS '小计',
    @total := @total + min_total AS '总计'
FROM
    ( SELECT date( reg_time ) AS reg_time,
    sum( reg_count ) AS min_total FROM t_reg GROUP BY date( reg_time ) ) AS temp,
    ( SELECT @total := 0 ) AS T1
ORDER BY
    reg_time;

Explain: SELECT @total: = 0 ,, phrase mean value is provided to the temporary variable @total 0;

So to meet the needs of the outcome of the above.

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159564.htm