0

我有 5 张桌子,如下所示。我怎么可能在 mysql 查询语句中获得所有没有培训课程数据(这意味着没有女性或男性学生)的培训公司的列表?

select training_companies.*
from training_companies
left join training_centers on training_centers.training_company_id = training_companies.id
left join training_center_programmes on training_centers.id = training_center_programmes.training_center_id
left join training_class_data on training_class_data.training_center_programme_id = training_center_programmes.id
where training_companies.id IS NULL

到目前为止,我只能到达这里,但我认为这是错误的。好心提醒。谢谢。

training_class_data
id | student_category_id | training_centre_programme_id | female | male
1  | 1                   | 1                            | 10     | 10
2  | 1                   | 2                            | 10     | 10
3  | 2                   | 1                            | 10     | 10
4  | 3                   | 1                            | 10     | 10

training_programmes
id | name
1  | Yoga
2  | Pilates

training_center_programmes
id | training_center_id   | status | training_programme_id
1  | 1                    | 1      | 1
2  | 2                    | 1      | 1
3  | 3                    | 1      | 1
4  | 4                    | 1      | 2
5  | 5                    | 1      | 2

training_centers
id | name          | address | postal code | training_company_id
1  | TF Center 1   | abc     | 1234        | 1
2  | TF Center 2   | abc     | 1234        | 1
3  | TF Center 3   | abc     | 1234        | 1
4  | SFT Center 1  | xyz     | 2345        | 2
5  | SFT Center 2  | xyz     | 2345        | 2
6  | KFT Center 1  | cbd     | 4234        | 3

training_companies
id | name              | address | postal code
 1 | Trim Fitness      | abc     | 1234
 2 | Stay Fit Training | xyz     | 2345  
 3 | Keep Fit Trainers | cbd     | 4234
4

1 回答 1

0

我怎么可能得到所有没有培训班数据的培训公司的名单

您只需要使用条件d.training_centre_programme_id IS NULL而不是training_companies.id IS NULL

select DISTINCT c.*
FROM training_companies              AS c
LEFT JOIN training_centers           AS tc ON tc.training_company_id         = c.id
LEFT JOIN training_center_programmes AS p  ON p.training_center_id           = tc.id
LEFT JOIN training_class_data        AS d  ON d.training_centre_programme_id = p.id
WHERE d.training_centre_programme_id IS NULL;

SQL 小提琴演示

于 2013-03-18T08:03:35.127 回答