Php 如何在此联接查询中省去重复的结果?
我有一个join查询返回以下结果: 我的经验教训是:Php 如何在此联接查询中省去重复的结果?,php,mysql,sql,Php,Mysql,Sql,我有一个join查询返回以下结果: 我的经验教训是: 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 -
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
...
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
...
在MySQL server 5.7上使用sql是否可以只显示一次每个日期?或者这应该在以后使用php完成?结果是这样的,因为一天可以教多节课,但一节课只属于一天
就像这样:我的课程:
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
...
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;
这并不完全是你想要的,也许有一种更优雅的方式可以做到这一点,但这里有一个想法:
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中……< /p> 数据库中的格式,而不是用来显示数据,因为你想把它们放在网页中,所以你必须找到一种方法从数据库中取出它们,然后根据你可以把它们呈现出来的方式。
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;
}
你可以试试。这将给出准确的结果
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
编辑您的问题并显示所需的结果。我宁愿在服务器端或客户端进行,因为这与数据表示相关,而不是提取相关。