使用Pivot表的Mysql查询

使用Pivot表的Mysql查询,mysql,pivot,Mysql,Pivot,我有最新消息。数据 empID Login Date TimeIN TimeOut 1001 01/01/2017 08:00 17:00 1001 01/02/2017 07:59 17:02 1001 01/03/2017 07:54 17:05 1001 01/04/2017 08:00 17:04 1001 01

我有最新消息。数据

empID Login Date TimeIN TimeOut 1001 01/01/2017 08:00 17:00 1001 01/02/2017 07:59 17:02 1001 01/03/2017 07:54 17:05 1001 01/04/2017 08:00 17:04 1001 01/05/2017 07:56 17:03 1001 01/06/2017 07:52 17:01 1001 01/07/2017 07:53 17:02 empID登录日期TimeIN超时 1001 01/01/2017 08:00 17:00 1001 01/02/2017 07:59 17:02 1001 01/03/2017 07:54 17:05 1001 01/04/2017 08:00 17:04 1001 01/05/2017 07:56 17:03 1001 01/06/2017 07:52 17:01 1001 01/07/2017 07:53 17:02 我希望输出像这样

EmpID mon tue wed thu fri 1001 08:00-17:00 7:59-17:02 07:54-17:05 08:00-17:04 07:56-17:03 星期一星期二星期三星期五 1001 08:00-17:00 7:59-17:02 07:54-17:05 08:00-17:04 07:56-17:03 我已尝试使用此查询

select cEmpID,dDate, (case DAYOFWEEK(dDate) WHEN 1 then CONCAT(cIn1,' - ',cOut2) else '' End) as 'Mon', (case DAYOFWEEK(dDate) WHEN 2 then CONCAT(cIn1,' - ',cOut2) else '' End) as 'Tue', (case DAYOFWEEK(dDate) WHEN 3 then CONCAT(cIn1,' - ',cOut2) else '' End) as 'Wed', (case DAYOFWEEK(dDate) WHEN 4 then CONCAT(cIn1,' - ',cOut2) else '' End) as 'Thu', (case DAYOFWEEK(dDate) WHEN 5 then CONCAT(cIn1,' - ',cOut2) else '' End) as 'Fri', (case DAYOFWEEK(dDate) WHEN 6 then CONCAT(cIn1,' - ',cOut2) else '' End) as 'Sat', (case DAYOFWEEK(dDate) WHEN 7 then CONCAT(cIn1,' - ',cOut2) else '' End) as 'Sun' from tblattenddetail Where cPeriodID='201702' GROUP BY cEmpID 选择cEmpID、dDate、, (案例DAYOFWEEK(dDate)当1然后CONCAT(cIn1,“-”,cOut2)else“结束)为“Mon”, (案例DAYOFWEEK(dDate)当2然后CONCAT(cIn1,“-”,cOut2)或者“结束”)作为“星期二”, (案例DAYOFWEEK(dDate)当3然后CONCAT(cIn1,“-”,cOut2)否则“结束)为“Wed”, (如DAYOFWEEK(dDate)为4,则CONCAT(cIn1,“-”,cOut2)为“结束”)为“Thu”, (案例DAYOFWEEK(dDate)为5,然后CONCAT(cIn1,“-”,cOut2)为“结束”)为“Fri”, (案例DAYOFWEEK(dDate)当6时,则CONCAT(cIn1,“-”,cOut2)否则“结束”)为“Sat”, (如DAYOFWEEK(dDate)为7,则CONCAT(cIn1,“-”,cOut2)为“结束”)为“Sun” 来自TBLATTENDETAIL cPeriodID='201702'的位置 按cEmpID分组
但它不起作用。

请尝试下面的查询-

SELECT empID,  
       MAX(case DAYOFWEEK(`Login Date`) WHEN 1 then CONCAT(TimeIN,' - ',TimeOut) else '' End) as 'Mon',
       MAX(case DAYOFWEEK(`Login Date`) WHEN 2 then CONCAT(TimeIN,' - ',TimeOut) else '' End) as 'Tue',
       MAX(case DAYOFWEEK(`Login Date`) WHEN 3 then CONCAT(TimeIN,' - ',TimeOut) else '' End) as 'Wed',
       MAX(case DAYOFWEEK(`Login Date`) WHEN 4 then CONCAT(TimeIN,' - ',TimeOut) else '' End) as 'Thu',
       MAX(case DAYOFWEEK(`Login Date`) WHEN 5 then CONCAT(TimeIN,' - ',TimeOut) else '' End) as 'Fri',
       MAX(case DAYOFWEEK(`Login Date`) WHEN 6 then CONCAT(TimeIN,' - ',TimeOut) else '' End) as 'Sat',
       MAX(case DAYOFWEEK(`Login Date`) WHEN 7 then CONCAT(TimeIN,' - ',TimeOut) else '' End) as 'Sun'  
FROM login
GROUP BY empID

这是供您参考的提琴-

当您不想将dDate放入结果时,为什么要在选择列表中拉dDate?请检查此答案,
https://stackoverflow.com/questions/42408465/need-to-convert-columns-into-rows-in-mysql/42409047#42409047
@chiragsatapara,此查询只能使用case解决。这里不需要使用pivot。