Mysql 可预约

Mysql 可预约,mysql,Mysql,我有两张mysql表,日程和约会。在计划表中,定义了医生终端,在其他表中,有预约。问题是医生总是可以更改他的终端,我不能将引用粘贴到预约表中,我只想在一个mysql查询中为两位医生获取可用的终端。有可能吗 CREATE TABLE `schedule` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `from` timestamp NULL DEFAULT NULL, `to` timestamp NULL DEFAULT NULL

我有两张mysql表,日程和约会。在计划表中,定义了医生终端,在其他表中,有预约。问题是医生总是可以更改他的终端,我不能将引用粘贴到预约表中,我只想在一个mysql查询中为两位医生获取可用的终端。有可能吗

CREATE TABLE `schedule` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `from` timestamp NULL DEFAULT NULL,
  `to` timestamp NULL DEFAULT NULL,
  `doctor_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;


INSERT INTO `schedule` (`id`, `from`, `to`, `doctor_id`)
VALUES
    (1, '2012-09-06 14:00:00', '2012-09-06 14:59:59', 1),
    (2, '2012-09-06 15:00:00', '2012-09-06 15:59:59', 1),
    (3, '2012-09-06 16:00:00', '2012-09-06 16:59:59', 1),
    (4, '2012-09-06 17:00:00', '2012-09-06 17:59:59', 1),
    (5, '2012-09-06 14:00:00', '2012-09-06 14:59:59', 2),
    (6, '2012-09-06 15:00:00', '2012-09-06 15:59:59', 2),
    (7, '2012-09-06 16:00:00', '2012-09-06 16:59:59', 2),
    (8, '2012-09-06 17:00:00', '2012-09-06 17:59:59', 2);

CREATE TABLE `appointments` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `from` timestamp NULL DEFAULT NULL,
  `to` timestamp NULL DEFAULT NULL,
  `doctor_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;


INSERT INTO `appointments` (`id`, `from`, `to`, `doctor_id`)
VALUES
    (3, '2012-09-06 16:00:00', '2012-09-06 16:59:59', 1),
    (4, '2012-09-06 17:00:00', '2012-09-06 17:59:59', 1),
    (5, '2012-09-06 14:00:00', '2012-09-06 14:59:59', 2),
    (8, '2012-09-06 17:00:00', '2012-09-06 17:59:59', 2);

如果保证约会id等于日程id,则:

select `id`,`from`,`to`,`doctor_id`
from schedule
where `id` not in 
(select `id` from appointments)
order by `doctor_id`,`from` 

如果不能保证将约会id链接,则:

select `id`,`from`,`to`,`doctor_id`
from schedule
where `id` not in 
(select `t1`.`id` 
from(
select `s`.`id`,`s`.`from`,`s`.`to`,`s`.`doctor_id` 
from schedule as s 
inner join 
appointments as a
on `s`.`doctor_id`=`a`.`doctor_id`
and `s`.`from`=`a`.`from`
and `s`.`to`=`a`.`to`)
as t1)

如果保证约会id等于日程id,则:

select `id`,`from`,`to`,`doctor_id`
from schedule
where `id` not in 
(select `id` from appointments)
order by `doctor_id`,`from` 

如果不能保证将约会id链接,则:

select `id`,`from`,`to`,`doctor_id`
from schedule
where `id` not in 
(select `t1`.`id` 
from(
select `s`.`id`,`s`.`from`,`s`.`to`,`s`.`doctor_id` 
from schedule as s 
inner join 
appointments as a
on `s`.`doctor_id`=`a`.`doctor_id`
and `s`.`from`=`a`.`from`
and `s`.`to`=`a`.`to`)
as t1)

术语“termin”是什么意思?你能解释一下吗?另外,你问了很多问题,但没有接受任何答案。若要接受回答,请单击绿色复选框。约会id是否始终是它链接到的计划行的id?id不相同,我只将粘贴记录从一个表复制到另一个表。“termin”是什么意思?你能解释一下吗?另外,你问了很多问题,但没有接受任何答案。若要接受回答,请单击绿色复选框。约会id是否始终是它链接到的计划行的id?id不相同,我仅将粘贴记录从一个表复制到另一个表。