在mysql中如何选择联合连接中的列

时间:2017-06-29 04:08:15

标签: mysql join union

当我使用union时,如何选择连接表的列。具体如何在以下查询中选择b.id(预订ID)?这也是正确的方法吗?你能告诉我其他方法吗?我还想选择所选日期的可用时间(如果有的话)。我有三种时间安排。

  

cat1(类别1):每周特定日期的可用性(例如:任何月份的周一,周二,周三等)

     

cat2(类别2):任何月份的第一个星期一,第二个星期六等的可用性。

     

cat3(类别3):上周六,上周五等的可用性      任何一个月。

     

我在doctor_schedule表中使用day_offset和cat列来计算日期并将其检查到@selected_date。我也在离开日子的过程中过滤了几天(off_days表存储任何假期的信息,或者医生因个人原因休假)。

mysql> select * from doctor;
    +----+-------------+
    | id | name        |
    +----+-------------+
    |  1 | John Doe    |
    |  2 | Larry Jones |
    +----+-------------+

mysql> select * from doctor_schedule;
+----+-----------+-----+------------+----------+---------------+--------+------------+-----+
| id | doctor_id | day | start_time | end_time | booking_limit | active | day_offset | cat |
+----+-----------+-----+------------+----------+---------------+--------+------------+-----+
|  1 |         2 |   5 | 10:00:00   | 12:00:00 |             1 |      1 |          3 |   2 |
|  2 |         2 |   5 | 19:00:00   | 22:00:00 |             1 |      1 |          3 |   2 |
|  3 |         2 |   6 | 19:00:00   | 22:00:00 |             1 |      1 |          0 |   3 |
+----+-----------+-----+------------+----------+---------------+--------+------------+-----+

mysql> select * from booking;
+----+---------+-------------+--------------+------+
| id | user_id | schedule_id | booking_date | paid |
+----+---------+-------------+--------------+------+
|  1 |       1 |           3 | 2017-06-26   |    1 |
+----+---------+-------------+--------------+------+

mysql> select * from off_day;
+----+-----------+-------------+------------+
| id | doctor_id | schedule_id | date       |
+----+-----------+-------------+------------+
|  2 |         1 |           3 | 2017-06-26 |
+----+-----------+-------------+------------+

set @selected_date := "2017-06-26";
set @doctor_id := 2;
SET @first_day = DATE_SUB(@selected_date, INTERVAL DAYOFMONTH(@selected_date) - 1 DAY);

select s.*
 from
(select
    s1.id
from doctor_schedule s1
where
    s1.cat = 1
and
    s1.day = weekday(@selected_date)
UNION
select
    s2.id
from doctor_schedule s2
where
    s2.cat = 2
and
     DATE_ADD(@first_day, INTERVAL (s2.day - WEEKDAY(@first_day)) + (s2.other*7) DAY) = @selected_date
UNION
select
    s3.id
from doctor_schedule s3
where
    s3.cat = 3
and
    date_sub(LAST_DAY(@selected_date), INTERVAL ((7 + WEEKDAY(LAST_DAY(@selected_date)) - s3.other) % 7) DAY) = @selected_date
) as s
right join
    booking b
on
    s.id = b.schedule_id
and 
    b.booking_date >= @selected_date
and
    b.paid = 1
left join
    off_day o
on
    s.id = o.schedule_id
and
    o.date = @selected_date
and
    o.doctor_id = @doctor_id
where
    o.schedule_id is null

group by
    s.id

1 个答案:

答案 0 :(得分:0)

先生。 Tim Biegeleisen指出我的查询中没有booking_id。感谢他,我找到了解决方案。它应该是 b.id

set @selected_date := "2017-06-26";
    set @doctor_id := 2;
    SET @first_day = DATE_SUB(@selected_date, INTERVAL 

DAYOFMONTH(@selected_date) - 1 DAY);

select s.*, b.id
 from
(select
    s1.id
from doctor_schedule s1
where
    s1.cat = 1
and
    s1.day = weekday(@selected_date)
UNION
select
    s2.id
from doctor_schedule s2
where
    s2.cat = 2
and
     DATE_ADD(@first_day, INTERVAL (s2.day - WEEKDAY(@first_day)) + (s2.other*7) DAY) = @selected_date
UNION
select
    s3.id
from doctor_schedule s3
where
    s3.cat = 3
and
    date_sub(LAST_DAY(@selected_date), INTERVAL ((7 + WEEKDAY(LAST_DAY(@selected_date)) - s3.other) % 7) DAY) = @selected_date
) as s
right join
    booking b
on
    s.id = b.schedule_id
and 
    b.booking_date >= @selected_date
and
    b.paid = 1
left join
    off_day o
on
    s.id = o.schedule_id
and
    o.date = @selected_date
and
    o.doctor_id = @doctor_id
where
    o.schedule_id is null

group by
    s.id