我有一个连接查询返回这些结果:
我的经验教训:
2017-04-02 : 10:00:00 - 12:00:00
2017-04-02 : 15:00:00 - 16:30:00
2017-04-02 : 17:00:00 - 18:00:00
2017-04-02 : 18:10:00 - 19:10:00
2017-04-03 : 10:00:00 - 12:00:00
2017-04-04 : 10:00:00 - 12:00:00
2017-04-05 : 17:45:00 - 18:45:00
2017-04-08 : 08:50:00 - 10:20:00
2017-04-08 : 10:30:00 - 12:00:00
2017-04-08 : 17:30:00 - 18:30:00
...
是否可以在MySQL服务器5.7上使用sql显示每个日期一次?或者这应该在以后使用PHP完成?结果是这样的,因为它可以在一天内教授多个课程,但是一节课只属于一天。
像这样:我的经验教训:
2017-04-02 : 10:00:00 - 12:00:00
15:00:00 - 16:30:00
17:00:00 - 18:00:00
18:10:00 - 19:10:00
2017-04-03 : 10:00:00 - 12:00:00
2017-04-04 : 10:00:00 - 12:00:00
2017-04-05 : 17:45:00 - 18:45:00
2017-04-08 : 08:50:00 - 10:20:00
10:30:00 - 12:00:00
17:30:00 - 18:30:00
...
这是查询。我尝试为日期连接做一个子查询,但我无法让它工作。
SELECT
lessons_date,
start_times,
end_times
FROM lessons
JOIN lesson_date
ON id_lessons_date = lesson_date_id_lessons_date
JOIN start_time
ON id_start_times = start_time_has_end_time_start_time_id_start_times
JOIN end_time
ON id_end_times = start_time_has_end_time_end_time_id_end_times;
答案 0 :(得分:1)
这并不是你想要的,而且可能有更优雅的方式来做到这一点,但这里有一个想法:
SELECT
CreatedOn =
CASE
WHEN name is null
THEN CreatedOn
ELSE null
END
, Name
FROM (
select distinct
createdon
,null as name
,createdon as c2
FROM account
UNION
SELECT
a.createdon
,a.name
,a.createdon as c2
FROM account a
JOIN account b on a.createdon = b.createdon
) x
ORDER BY c2, name
返回:
您可能需要考虑在PHP中格式化它...
答案 1 :(得分:1)
数据库不用于显示数据,因为您希望在网页中显示它们,因此您必须找到从数据库中获取它们的方法,然后根据提取方式显示它们。
SELECT
lessons_date,
group_concat(start_times SEPARATOR ',') start_times,
group_concat(end_times SEPARATOR ',') end_times
FROM lessons
JOIN lesson_date
ON id_lessons_date = lesson_date_id_lessons_date
JOIN start_time
ON id_start_times = start_time_has_end_time_start_time_id_start_times
JOIN end_time
ON id_end_times = start_time_has_end_time_end_time_id_end_times;
GROUP BY lessons_date
然后在PHP
中添加类似:
$start_times = explode(",", $row["start_times"]); // array of times
for ($time as $start_times) {
echo $time;
}
答案 2 :(得分:0)
你可以尝试一下。这将给出确切的结果。
SELECT
temp.lessons_date ,
GROUP_CONCAT(start_times SEPARATOR '<br/>') ,
GROUP_CONCAT(end_times SEPARATOR '<br/>') FROM (
SELECT
lessons_date,
start_times,
end_times
FROM lessons
JOIN lesson_date
ON id_lessons_date = lesson_date_id_lessons_date
JOIN start_time
ON id_start_times = start_time_has_end_time_start_time_id_start_times
JOIN end_time
ON id_end_times = start_time_has_end_time_end_time_id_end_times ORDER BY temp.lessons_date
) temp GROUP BY temp.lessons_date