根据其他事件中的发生情况选择用户

时间:2018-07-27 16:52:47

标签: mysql sql

我正在创建一个选择查询,供用户选择特定日期的位置。由于每个位置均具有最大容量,因此我想使用MySQL来显示查询中房间是否已满。

这是我当前表格的布局:

餐桌员工

location | tid  |  capacity  
---------------------------
A101     | 1234 |  5
A103     | 2345 |  2
B105     | 3456 |  3
Patio    | 4567 |  10

表格“时间表”

date       | sid | plan
---------------------------
2018-07-30 | 1   | 2345
2018-07-30 | 2   | 1234
2018-07-30 | 3   | 2345
2018-07-30 | 4   | 4567
2018-07-31 | 1   | 3456
2018-07-31 | 2   | 3566
2018-07-31 | 3   | 1234
2018-07-31 | 4   | 3456

请注意,A103(2345)已于7月30日预订满,B105(3456)也已于7月31日预订。

这是SELECT查询的预期结果,其中每个日期组稍后都会产生一个单独的下拉菜单,供用户选择位置(显示哪些位置已满):

date       | location | tid  | full
---------------------------
2018-07-30 | A101     | 1234 | No
2018-07-30 | A103     | 2345 | Yes
2018-07-30 | B105     | 3456 | No
2018-07-30 | Patio    | 4567 | No
2018-07-31 | A101     | 1234 | No
2018-07-31 | A103     | 2345 | No
2018-07-31 | B105     | 3456 | Yes
2018-07-31 | Patio    | 4567 | No

如何建立决赛桌?我很难全神贯注于应该如何加入/分组/计算每一行。

3 个答案:

答案 0 :(得分:0)

请考虑以下内容...

DROP TABLE IF EXISTS locations;

CREATE TABLE locations
(location_id SERIAL PRIMARY KEY
,capacity  INT NOT NULL
);

INSERT INTO locations VALUES
(1234, 5),
(2345, 2),
(3456, 3),
(4567,10);

DROP TABLE IF EXISTS schedule;

CREATE TABLE schedule
(schedule_id SERIAL PRIMARY KEY
,date DATE NOT NULL 
,location_id INT NOT NULL
);

INSERT INTO schedule VALUES
(1,'2018-07-30',2345),
(2,'2018-07-30',1234),
(3,'2018-07-30',2345),
(4,'2018-07-30',4567),
(5,'2018-07-31',3456),
(6,'2018-07-31',3566),
(7,'2018-07-31',1234),
(8,'2018-07-31',3456);

SELECT l.*
     , s.date
     , COUNT(s.schedule_id) >= l.capacity full 
  FROM locations l 
  LEFT 
  JOIN schedule s 
    ON s.location_id = l.location_id 
 GROUP 
    BY l.location_id
     , s.date;
+-------------+----------+------------+------+
| location_id | capacity | date       | full |
+-------------+----------+------------+------+
|        1234 |        5 | 2018-07-30 |    0 |
|        1234 |        5 | 2018-07-31 |    0 |
|        2345 |        2 | 2018-07-30 |    1 |
|        3456 |        3 | 2018-07-31 |    0 |
|        4567 |       10 | 2018-07-30 |    0 |
+-------------+----------+------------+------+
5 rows in set (0.00 sec)

答案 1 :(得分:0)

如果您的mysql不支持Windows功能。

  1. 使用CROSS JOIN来制作staffschedule.date结果集。
  2. selectcount中写一个子查询,date的累积计算

TestDLL

create table schedule(
   date date,
    sid int,
    plan int
);

insert into schedule values ('2018-07-30', 1 ,2345);
insert into schedule values ('2018-07-30', 2 ,1234);
insert into schedule values ('2018-07-30', 3 ,2345);
insert into schedule values ('2018-07-30', 4 ,4567);
insert into schedule values ('2018-07-31', 1 ,3456);
insert into schedule values ('2018-07-31', 2 ,3566);
insert into schedule values ('2018-07-31', 3 ,1234);
insert into schedule values ('2018-07-31', 4 ,3456);

查询

SELECT t1.date, 
       st.*,
       ( CASE 
           WHEN   (SELECT IFNULL(SUM(sc1.sid),0)
                 FROM   schedule sc1 
                 WHERE  sc1.date = t1.date 
                        AND sc1.PLAN = st.tid) < capacity THEN 'NO' 
           ELSE 'YES' 
         END ) full

FROM   (SELECT DISTINCT date 
        FROM   schedule) t1 
       CROSS JOIN staff st 

[结果]

|       date | location |  tid | full |
|------------|----------|------|------|
| 2018-07-30 |     A101 | 1234 |   NO |
| 2018-07-30 |     A103 | 2345 |  YES |
| 2018-07-30 |     B105 | 3456 |   NO |
| 2018-07-30 |    Patio | 4567 |   NO |
| 2018-07-31 |     A101 | 1234 |   NO |
| 2018-07-31 |     A103 | 2345 |   NO |
| 2018-07-31 |     B105 | 3456 |  YES |
| 2018-07-31 |    Patio | 4567 |   NO |

sqlfiddle

答案 2 :(得分:0)

我将使用cross join来生成行。然后group by和其他逻辑来填写详细信息:

select d.date, s.tid,
       (case when count(sc.plan) < s.capacity then 'no' else 'yes' end) as is_full
from (select distinct date from schedule s) d cross join
     staff s left join
     schedule sc
     on sc.date = d.date and sc.plan = s.tid
group by d.date, s.tid, s.capacity;