一道MySQL面试题

表结构

CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `action_time` datetime DEFAULT NULL,
  `action_type` varchar(255) CHARACTER SET utf8mb4 DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

数据

INSERT INTO `test`.`test`(`id`, `user_id`, `action_time`, `action_type`) VALUES (1, 1001, '2019-05-15 09:01:06', 'ACTIVATE');
INSERT INTO `test`.`test`(`id`, `user_id`, `action_time`, `action_type`) VALUES (2, 1002, '2019-05-15 09:02:06', 'LOGIN');
INSERT INTO `test`.`test`(`id`, `user_id`, `action_time`, `action_type`) VALUES (3, 1002, '2019-05-15 09:06:06', 'PURCHASE');
INSERT INTO `test`.`test`(`id`, `user_id`, `action_time`, `action_type`) VALUES (4, 1003, '2019-05-16 09:10:06', 'RESIGTER');
INSERT INTO `test`.`test`(`id`, `user_id`, `action_time`, `action_type`) VALUES (5, 1001, '2019-05-16 09:12:06', 'LOGIN');
INSERT INTO `test`.`test`(`id`, `user_id`, `action_time`, `action_type`) VALUES (6, 1003, '2019-05-16 09:15:06', 'LOGIN');

一道MySQL面试题_第1张图片
某天活跃用户定义,在这一天action记录既是活跃用户

  1. 查出每一天的活跃用户量,请写出SQL
  2. 查出每一天不同action_type的用户量,请写SQL
SELECT count(*) total,action_type,DATE_FORMAT(action_time,'%y-%m-%d')
FROM TEST GROUP BY DATE_FORMAT(action_time,'%y-%m-%d'),action_type;

一道MySQL面试题_第2张图片

你可能感兴趣的:(mysql)