我有一个MySQL表,如下所示:
# Structure for table "audit"
#
DROP TABLE IF EXISTS `audit`;
CREATE TABLE `audit` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`item` varchar(255) DEFAULT NULL,
`amount` varchar(255) DEFAULT NULL,
`myDate` date DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;
#
# Data for table "audit"
#
INSERT INTO `audit` VALUES
(1,'audit','0','2015-12-23'),
(2,'fs_delete','4206','2015-12-23'),
(3,'log_print','27443','2015-12-23'),
(4,'sys_error','10102','2015-12-23'),
(5,'sys_info','150','2015-12-23'),
(6,'sys_proxy','30','2015-12-23'),
(7,'users','2','2015-12-23'),
(8,'audit','7','2015-12-22'),
(9,'fs_delete','4206','2015-12-22'),
(10,'log_print','27443','2015-12-22'),
(11,'sys_error','10102','2015-12-22'),
(12,'sys_info','150','2015-12-22'),
(13,'sys_proxy','30','2015-12-22'),
(14,'users','2','2015-12-22');
我想输出如下图所示
答案 0 :(得分:0)
我认为您需要这样的查询:
SELECT `myDate`
, MAX(CASE WHEN `item` = 'audit' THEN `amount` END) AS `audit`
, MAX(CASE WHEN `item` = 'fs_delete' THEN `amount` END) AS `fs_delete`
, MAX(CASE WHEN `item` = 'log_print' THEN `amount` END) AS `log_print`
, MAX(CASE WHEN `item` = 'sys_error' THEN `amount` END) AS `sys_error`
, MAX(CASE WHEN `item` = 'sys_info' THEN `amount` END) AS `sys_info`
FROM `audit`
GROUP BY `myDate`;