简体   繁体   English

如何计算另一个表中的行以影响另一个表

[英]How to count rows from another table to affect another table

I have the following tables and have been able to get a result of classes that start after a particular time on a certain date. 我有下表,并能够得到在特定日期的特定时间之后开始的类的结果。 What I want to do is somehow only show classes that have less than 10 bookings associated. 我想做的是以某种方式仅显示预订少于10个的班级。

I have created this sql query to get the classes that a particular user is able to book, but I want to hide the classes where there are already 10 people already booked in the class in question. 我创建了此sql查询,以获取特定用户可以预订的课程,但是我想隐藏该课程中已经有10个人预订的课程。 Ie the class is full if there are 10 or more associated bookings so I dont want to show these classes. 也就是说,如果有10个或更多的相关预订,则该班级已满,因此我不想显示这些班级。

Any help would be much appreciated. 任何帮助将非常感激。

SELECT DISTINCT b.name
              , a.time 
           FROM class a   
           JOIN class_detail b 
             ON a.class_id = b.id   
           JOIN branch c 
             ON a.branch_id = c.id 
          WHERE c.level <= ( SELECT d.level 
                               FROM client d 
                              WHERE d.facebook_id = 'xxxxxx'
                           )
            AND a.date = '2016-08-17' 
            AND a.time >= '13.00.00';

BOOKINGS
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id        | bigint(20)  | NO   | PRI | NULL    |       |
| CLIENT_ID | int(11)     | NO   |     | NULL    |       |
| CLASS_ID  | int(11)     | NO   |     | NULL    |       |
| STATUS    | varchar(10) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

mysql> show fields from BRANCH;
+---------------------+-------------+------+-----+---------+----------------+
| Field               | Type        | Null | Key | Default | Extra          |
+---------------------+-------------+------+-----+---------+----------------+
| id                  | int(10)     | NO   | PRI | NULL    | auto_increment |
| NAME                | char(50)    | NO   |     | NULL    |                |
| CONTACT_NO          | char(50)    | YES  |     | NULL    |                |
| MAP_IMG_PATH        | char(200)   | YES  |     | NULL    |                |
| ADDRESS             | char(200)   | YES  |     | NULL    |                |
| LEVEL               | int(2)      | NO   |     | NULL    |                |
| LOCATION            | int(10)     | YES  |     | NULL    |                |
| SECTOR_NAME         | varchar(45) | YES  |     | NULL    |                |
| SECTOR_MAP_IMG_PATH | char(200)   | YES  |     | NULL    |                |
+---------------------+-------------+------+-----+---------+----------------+



mysql> show fields from CLIENT;
+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| id           | int(10)     | NO   | PRI | NULL    |       |
| NAME         | char(50)    | NO   |     | NULL    |       |
| DOB          | int(8)      | NO   |     | NULL    |       |
| LOCAL_BRANCH | int(10)     | YES  |     | NULL    |       |
| FACEBOOK_ID  | char(50)    | NO   |     | NULL    |       |
| START_DATE   | int(8)      | NO   |     | NULL    |       |
| EMAIL        | char(50)    | YES  |     | NULL    |       |
| PIN          | int(4)      | YES  |     | NULL    |       |
| END_DATE     | int(8)      | NO   |     | NULL    |       |
| LEVEL        | int(2)      | YES  |     | NULL    |       |
| TEL          | varchar(20) | YES  |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+



mysql> show fields from CLASS_DETAIL;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | int(10)      | NO   | PRI | NULL    |       |
| NAME         | char(50)     | NO   |     | NULL    |       |
| DESCRIPTION  | char(200)    | NO   |     | NULL    |       |
| CATEGORY     | varchar(4)   | YES  |     | NULL    |       |
| ACHIEVE_TYPE | char(200)    | YES  |     | NULL    |       |
| IMG_M        | varchar(200) | YES  |     | NULL    |       |
| IMG_F        | varchar(200) | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+



mysql> show fields from CLASS;
+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| id        | int(11) | NO   | PRI | NULL    | auto_increment |
| CLASS_ID  | int(10) | YES  |     | NULL    |                |
| BRANCH_ID | int(10) | NO   |     | NULL    |                |
| DURATION  | int(3)  | YES  |     | NULL    |                |
| DATE      | date    | NO   |     | NULL    |                |
| TIME      | time    | NO   |     | NULL    |                |
| STATUS    | char(1) | NO   |     | NULL    |                |
+-----------+---------+------+-----+---------+----------------+
7 rows in set (0.11 sec)

Here is how you can do this without making changes to your tables. 这是无需更改表即可执行的操作。

SELECT DISTINCT b.name
              , a.time 
           FROM class a 
           Inner join (SELECT class_id, count(clientid) 
                       FROM bookings 
                       GROUP BY class_id
                       HAVING count(clientid) < 10) as openClasses on        
                a.class_id = openClasses.class_id
           JOIN class_detail b 
             ON a.class_id = b.id   
           JOIN branch c 
             ON a.branch_id = c.id 
          WHERE c.level <= ( SELECT d.level 
                               FROM client d 
                              WHERE d.facebook_id = 'xxxxxx'
                           )
            AND a.date = '2016-08-17' 
            AND a.time >= '13.00.00';

This query uses a derived table, which I called "openClasses". 该查询使用派生表,我称之为“ openClasses”。 The point of the table is to get the class_ids of the classes that have less than 10 bookings. 该表的重点是获取预订少于10个的类的class_id。 This table is then inner joined to restrict results to only this set of classes. 然后将该表内部联接以将结果限制为仅此组类。

It may be that you have to add a where clause into this derived table to restrict to only certain statuses. 可能是您必须在此派生表中添加where子句以仅限制为某些状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM