gpt4 book ai didi

java - MySQL CASE使用如何删除重复的空列

转载 作者:行者123 更新时间:2023-11-29 12:28:00 26 4
gpt4 key购买 nike

我有 MySQL 问题。我创建了这个查询,但它显示如下;我在MySQL中使用了case函数。

这是代码

SELECT DISTINCT 
attendancehistory.CustomerFacilityId,
(CASE '01' WHEN SUBSTRING(`Date`, 8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS '01',
(CASE '02' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS '02',
(CASE '03' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS '03',
(CASE '04' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS '04',
(CASE '05' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS '05',
(CASE '06' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS '06'
FROM
attendancehistory
WHERE
`Employee id`='1'
GROUP BY
Id;

输出如下

CuFId | 01  |  02 |  03 |  04  |  05
1 | D | Null| Null| Null| Null
533 | N | Null| Null| Null | Null
533 | Null| D | Null| Null | Null
533 | Null| Null| N | Null | Null
533 | Null| Null| Null| Null | D

但实际上我需要这样

CuFId | 01  | 02  | 03  | 04  | 05
1 | D | Null| Null| Null| Null
533 | N | D | N | Null| D

这里是示例代码:

CREATE TABLE `attendancehistory` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`LoginId` int(11) NOT NULL DEFAULT '0',
`Employee id` int(10) NOT NULL DEFAULT '0',
`CustomerFacilityId` int(11) NOT NULL DEFAULT '0',
`Date` varchar(50) DEFAULT NULL,
`Day` varchar(11) DEFAULT NULL,
`Night` varchar(11) DEFAULT NULL,
`EmpAttendaceTotalMonth` varchar(11) DEFAULT NULL,
`EntryDate` varchar(50) DEFAULT NULL,
PRIMARY KEY (`Id`),
KEY `FKAttendance494326` (`LoginId`),
KEY `FKAttendance87961` (`CustomerFacilityId`),
CONSTRAINT `FKAttendance494326` FOREIGN KEY (`LoginId`) REFERENCES `login` (`Id`),
CONSTRAINT `FKAttendance87961` FOREIGN KEY (`CustomerFacilityId`) REFERENCES `customerfacility` (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1;

/*Data for the table `attendancehistory` */

insert into `attendancehistory`
(`Id`,`LoginId`,`Employee id`,`CustomerFacilityId`,`Date`,`Day`,`Night`,`EmpAttendaceTotalMonth`,`EntryDate`) values
(3,1,1,1,'2015-1-01','1','0','2015-1','2015-01-14'),
(4,1,1,533,'2015-1-01','0','1','2015-1','2015-01-14'),
(5,1,1,533,'2015-1-02','1','1','2015-1','2015-01-14'),
(6,1,1,533,'2015-1-03','0','1','2015-1','2015-01-14'),
(7,1,1,533,'2015-1-05','1','0','2015-1','2015-01-14');

最佳答案

大概,您想要这个:

SELECT ah.CustomerFacilityId,
MAX( CASE '01' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS `01`,
MAX(CASE '02' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS `02`,
MAX(CASE '03' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS `03`,
MAX(CASE '04' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS `04`,
MAX(CASE '05' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS `05`,
MAX(CASE '06' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-')) ELSE NULL END) AS `06`
FROM attendancehistory ah
WHERE `Employee id` = 1
GROUP BY Id;

请注意,不应在列名称或整数常量中使用单引号。此外,您可以仅使用 case 语句来简化此逻辑,但如果没有示例数据和/或 SQL Fiddle,尝试更改它很容易出错。

关于java - MySQL CASE使用如何删除重复的空列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28017026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com