我正在开发基于时间段的PHP预订系统。 我已经设置了4个数据库表!
用户(存储所有成员详细信息)
ID_BOOKSLOT ID_USER ID_THERAPIST ID_TIMESLOT
1 10 1 1 (09:00) 2 11 2 1 (09:00) 3 12 3 2 (09:15) 4 15 3 1 (09:00)
现在,我的问题是,当我想要回显数据时,它会继续显示时间段的重复:
thera a thera b thera c
-------------------------------------------------
09:00 BOOKED available available
09:00 available BOOKED available
09:00 available available BOOKED
09:15 available BOOKED available
如你所见,09:00显示三次,我想要下面的内容
thera a thera b thera c
-------------------------------------------------
09:00 BOOKED BOOKED BOOKED
09:15 available BOOKED available
加入桌子或其他方面可能有问题。 加入表的代码
$mysqli->query("SELECT * FROM bookslot RIGHT JOIN timeslot ON bookslot.id_timeslot = timeslot.id_timeslot LEFT JOIN therapist ON bookslot.id_therapist = therapist.id_therapist"
如果有人有这个系统的解决方案,请帮助我,我很喜欢它!
答案 0 :(得分:2)
select
id_TimeSlot
, coalesce(Thera_A, 'available') as Thera_A
, coalesce(Thera_B, 'available') as Thera_B
, coalesce(Thera_C, 'available') as Thera_C
from
(
select
t.id_TimeSlot
, max(case b.id_Therapist when 1 then 'booked' else null end) as Thera_A
, max(case b.id_Therapist when 2 then 'booked' else null end) as Thera_B
, max(case b.id_Therapist when 3 then 'booked' else null end) as Thera_C
from TimeSlot as t
left join BookSlot as b on b.id_TimeSlot = t.id_TimeSlot
left join Therapist as p on p.id_Therapist = b.id_Therapist
group by t.id_TimeSlot
) as xx ;
测试:
create table TimeSLot (id_TimeSLot integer);
create table Therapist (id_Therapist integer);
create table BookSlot (id_Therapist integer, id_TimeSlot integer);
insert into Therapist (id_Therapist)
values (1), (2), (3);
insert into TimeSlot (id_TimeSlot)
values (1), (2), (3), (4), (5);
insert into BookSlot (id_Therapist,id_TimeSlot)
values (1,1), (1,5), (2,1), (2,4), (3,1);
返回
id_TimeSlot Thera_A Thera_B Thera_C
----------------------------------------------
1 booked booked booked
2 available available available
3 available available available
4 available booked available
5 booked available available
答案 1 :(得分:1)
我想你需要GROUP BY id_timeslot,然后检查哪些治疗师被预订(或不是)。
为了避免复杂的查询,请创建表“约会”(id,u_id,t_id,start,stop,day)... 然后,您可以使用BETWEEN开始/停止和WHERE day =某天打印特定日期或时间跨度的约会......