mysql join添加新列

时间:2015-12-07 00:28:00

标签: php mysql

如何更改查询,以便它还添加来自“更新个人”的姓氏列。表?它听起来如此简单,但它似乎没有工作。我尝试使用patientId的完整外连接?

任何帮助都将受到高度赞赏。

$query = "select ts.theTime,d.doctorName, 
bMon.id as bMon, bTue.id as bTue, bWed.id as bWed, bThu.id as bThu, bFri.id as bFri 
from timeSlots ts 
cross join doctors d 
left join booking bMon 
on bMon.apptDate=@Monday and bMon.timeSlotId=ts.id and bMon.doctorId=d.doctorId 
left join booking bTue 
on bTue.apptDate=date_add(@Monday, INTERVAL 1 DAY) and bTue.timeSlotId=ts.id and bTue.doctorId=d.doctorId 
left join booking bWed 
on bWed.apptDate=date_add(@Monday, INTERVAL 2 DAY) and bWed.timeSlotId=ts.id and bWed.doctorId=d.doctorId 
left join booking bThu 
on bThu.apptDate=date_add(@Monday, INTERVAL 3 DAY) and bThu.timeSlotId=ts.id and bThu.doctorId=d.doctorId 
left join booking bFri 
on bFri.apptDate=date_add(@Monday, INTERVAL 4 DAY) and bFri.timeSlotId=ts.id and bFri.doctorId=d.doctorId 
cross join (select @Monday:='2015-11-30') params 
order by ts.theTime,d.doctorName;";

架构:

create table doctors 
(   doctorId int(4) primary key,
doctorName varchar(20) not null
);

create table timeSlots 
(
id int auto_increment primary key,
theTime time not null
);

insert into timeSlots (theTime) values ('09:00:00');    -- 1
insert into timeSlots (theTime) values ('10:00:00');    -- 2
insert into timeSlots (theTime) values ('11:00:00');    -- 3
insert into timeSlots (theTime) values ('12:00:00');
insert into timeSlots (theTime) values ('13:00:00');
insert into timeSlots (theTime) values ('14:00:00');
insert into timeSlots (theTime) values ('15:00:00');
insert into timeSlots (theTime) values ('16:00:00');
insert into timeSlots (theTime) values ('17:00:00');    -- 9

create table booking
(   surname varchar(20),
id int auto_increment primary key,
patientId int not null,
amORpm varchar(2),
doctorName varchar(20),
altamORpm varchar(1),
altdate varchar(1),
altdoctor varchar(1),
date date,
doctorId int not null,
apptTime time,
booked varchar(1)
apptDate date not null,
timeSlotId int not null,
doctorId int(4),
key(apptDate) -- index
);

create table updatepersonal (
surname varchar(20),
id int(4),
forename varchar(20),
DOB datetime,
doctorId  int not null,
contactno char(11),
email varchar(40),
address varchar(100),
PRIMARY KEY (id)
);

0 个答案:

没有答案