dongren1353 2015-12-07 00:28
浏览 45

mysql join添加新列

How can change the query so it also adds the surname column from 'updatepersonal' table? it sounds so simple but it doesnt seem to be working. I tried using a full outer join with the patientId?

Any help would be highly appreciated.

$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;";

Schema:

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀