我搜索了旧线程,但还没有找到以下问题的解决方案:是否可以在不诉诸嵌套查询的情况下对 mysql 结果进行分组和排列?
请参阅下面的示例。
我有:
Month Jan, Location USA, Program DDD
Month Jan, Location UK, Program EEE
Month Jan, Location USA, Program LLL
Month FEB, Location UAE, Program EEE
Month FEB, Location USA, Program DDD
Month FEB, Location UK, Program MMM
Month MAR, Location USA, Program FFF
Month MAR, Location UAE, Program FFF
Month MAR, Location UK, Program FFF
我希望它们像这样显示:
+---------++---------++---------+
|Month Jan||Month Feb||Month Mar|
+---------------+---------++---------++---------+
|Location USA | || || |
+---------------+---------++---------++---------+
| |DDD ||DDD ||FFF |
+---------------+---------++---------++---------+
| |EEE || || |
+---------------+---------++---------++---------+
| |LLL || || |
+---------------+---------++---------++---------+
|Location UK | || || |
+---------------+---------++---------++---------+
| |EEE ||MMM ||FFF |
+---------------+---------++---------++---------+
|Location UAE | || || |
+---------------+---------++---------++---------+
| | ||EEE ||FFF |
+---------------+---------++---------++---------+
我已尝试对查询进行分组,但无法将其呈现到 HTML 表中。
SELECT
event.`event_id`,
event.`event_program_id`,
event.`event_month`,
event.`event_location_id`,
location.`location_name`,
program.`program_name`,
program.`program_shortname`
FROM
`event`
LEFT JOIN
`location`
ON
event.`event_location_id` = location.`location_id`
LEFT JOIN
`program`
ON
event.`event_program_id` = program.`program_id`;
现在它的清单是这样的:
column1 column2 column3
line1 34
line2 34
line3 34
line5 34
我想要这样的地方:
column1 column2 column3
line1 34 34
34 34
34
line2 34
34 34 34
line3 34
line5 34