如何根据给定条件将行项目转换为列?
我正在尝试根据行条件转换行项目。例如,我有行项目1-31(从日期中提取的日期),我的条件是仅返回1-7天。我尝试了以下查询,但它返回整个行项目(第1-31天):
SELECT tblemployee.idnum AS ID_No, Concat(lname,", ",fname,"") AS Name,
MAX(IF(day(m_date)=1,basic,NULL)) BSC, MAX(IF(day(m_date)=1,ot,NULL)) OT,
MAX(IF(day(m_date)=2,basic,NULL)) BSC, MAX(IF(day(m_date)=2,ot,NULL)) OT,
MAX(IF(day(m_date)=3,basic,NULL)) BSC, MAX(IF(day(m_date)=3,ot,NULL)) OT,
MAX(IF(day(m_date)=4,basic,NULL)) BSC, MAX(IF(day(m_date)=4,ot,NULL)) OT,
MAX(IF(day(m_date)=5,basic,NULL)) BSC, MAX(IF(day(m_date)=5,ot,NULL)) OT,
MAX(IF(day(m_date)=6,basic,NULL)) BSC, MAX(IF(day(m_date)=6,ot,NULL)) OT,
MAX(IF(day(m_date)=7,basic,NULL)) BSC, MAX(IF(day(m_date)=7,ot,NULL)) OT,
MAX(IF(day(m_date)=8,basic,NULL)) BSC, MAX(IF(day(m_date)=8,ot,NULL)) OT,
MAX(IF(day(m_date)=9,basic,NULL)) BSC, MAX(IF(day(m_date)=9,ot,NULL)) OT,
MAX(IF(day(m_date)=10,basic,NULL)) BSC, MAX(IF(day(m_date)=10,ot,NULL)) OT,
MAX(IF(day(m_date)=11,basic,NULL)) BSC, MAX(IF(day(m_date)=11,ot,NULL)) OT,
MAX(IF(day(m_date)=12,basic,NULL)) BSC, MAX(IF(day(m_date)=12,ot,NULL)) OT,
MAX(IF(day(m_date)=13,basic,NULL)) BSC, MAX(IF(day(m_date)=13,ot,NULL)) OT,
MAX(IF(day(m_date)=14,basic,NULL)) BSC, MAX(IF(day(m_date)=14,ot,NULL)) OT,
MAX(IF(day(m_date)=15,basic,NULL)) BSC, MAX(IF(day(m_date)=15,ot,NULL)) OT,
MAX(IF(day(m_date)=16,basic,NULL)) BSC, MAX(IF(day(m_date)=16,ot,NULL)) OT,
MAX(IF(day(m_date)=17,basic,NULL)) BSC, MAX(IF(day(m_date)=17,ot,NULL)) OT,
MAX(IF(day(m_date)=18,basic,NULL)) BSC, MAX(IF(day(m_date)=18,ot,NULL)) OT,
MAX(IF(day(m_date)=19,basic,NULL)) BSC, MAX(IF(day(m_date)=19,ot,NULL)) OT,
MAX(IF(day(m_date)=20,basic,NULL)) BSC, MAX(IF(day(m_date)=20,ot,NULL)) OT,
MAX(IF(day(m_date)=21,basic,NULL)) BSC, MAX(IF(day(m_date)=21,ot,NULL)) OT,
MAX(IF(day(m_date)=22,basic,NULL)) BSC, MAX(IF(day(m_date)=22,ot,NULL)) OT,
MAX(IF(day(m_date)=23,basic,NULL)) BSC, MAX(IF(day(m_date)=23,ot,NULL)) OT,
MAX(IF(day(m_date)=24,basic,NULL)) BSC, MAX(IF(day(m_date)=24,ot,NULL)) OT,
MAX(IF(day(m_date)=25,basic,NULL)) BSC, MAX(IF(day(m_date)=25,ot,NULL)) OT,
MAX(IF(day(m_date)=26,basic,NULL)) BSC, MAX(IF(day(m_date)=26,ot,NULL)) OT,
MAX(IF(day(m_date)=27,basic,NULL)) BSC, MAX(IF(day(m_date)=27,ot,NULL)) OT,
MAX(IF(day(m_date)=28,basic,NULL)) BSC, MAX(IF(day(m_date)=28,ot,NULL)) OT,
MAX(IF(day(m_date)=29,basic,NULL)) BSC, MAX(IF(day(m_date)=29,ot,NULL)) OT,
MAX(IF(day(m_date)=30,basic,NULL)) BSC, MAX(IF(day(m_date)=30,ot,NULL)) OT,
MAX(IF(day(m_date)=31,basic,NULL)) BSC, MAX(IF(day(m_date)=31,ot,NULL)) OT
FROM tblmanhours INNER JOIN tblemployee
ON tblmanhours.idnum=tblemployee.idnum
WHERE m_date BETWEEN '2013-11-24' AND '2013-11-30'
GROUP BY tblemployee.idnum
ORDER BY Concat(tblemployee.lname,", ",tblemployee.fname,"");
我会添加什么条件来接收我的预期结果?
请帮忙。提前谢谢。