我有一个名为开关的MySQL表如下:
switchName switchCondition switchingTime
----------- --------------- -------------
First ON 10:00
First OFF 11:00
Second ON 10:30
Third OFF 13:00
Third ON 13:45
如何通过根据切换条件调整切换时间,使用上表以下列格式显示报告为html表?
switchName ON OFF
----------- ----- -------
First 10:00 11:00
Second 10:30 -
Third 13:45 13:00
答案 0 :(得分:3)
试试这个:
SELECT s.switchName,
MAX(CASE WHEN s.switchCondition = 'ON' THEN s.switchingTime ELSE '-' END) AS SwitchON,
MAX(CASE WHEN s.switchCondition = 'OFF' THEN s.switchingTime ELSE '-' END) AS SwitchOFF
FROM switches s
GROUP BY s.switchName;
答案 1 :(得分:1)
另一种方法
SELECT s.switchName, s1.switchingTime, s2.switchingTime
FROM switches s
LEFT JOIN switches s1 ON s.switchName = s1.switchName AND s1.switchCondition = 'ON'
LEFT JOIN switches s2 ON s.switchName = s2.switchName AND s2.switchCondition = 'OFF'
GROUP BY s.switchName
(虽然它代表您的短划线NULL
)