【发布时间】:2020-03-11 08:26:54
【问题描述】:
我有像第 1 区、第 1 a 区、第 1 b 区、第 1 c 区这样的区域以及在整个行业(即第 1 区)或任何子行业工作的多个有线电视运营商。我已经创建了有线电视运营商表,并希望将它们与区域进行映射。如果我设置区域表,如扇区 1、扇区 1 a、扇区 1 b、扇区 1 c,每个扇区都有自己的主键,那么如果我们必须让有线电视运营商工作,我如何在单行有线电视运营商中引用这些部门在那个特定的领域。
我的表结构如下:
运营商
| id | name
| 1 | 'abc'
| 2 | 'def'
| 3 | 'ghi'
地区
| id | name
| 1 | 'sector 1'
| 2 | 'sector 1a'
| 3 | 'sector 1b'
| 4 | 'sector 1c'
| 5 | 'sector 1d'
| 6 | 'sector 2'
| 7 | 'sector 2a'
| 8 | 'sector 2b'
| 9 | 'sector 2c'
| 10 | 'sector 2d'
我有operatorsareas 表,其中有地图运算符,其区域如下:
运营商领域
| op_id | area_id
| 1 | 1
| 2 | 1
| 3 | 1
| 1 | 7
| 2 | 8
| 3 | 7
现在我使用了这个没有结果的查询:
select o.id, o.name from operator as o
where not exists(select * from areas a where id in (1,7,8) and not exists(select * from operatorareas as oa where oa.operatorid=o.id
and oa.areaid = a.id))
我参考了以下链接:
SQL query through an intermediate table
我需要有关表格结构的指导。
【问题讨论】:
-
1) 您的意思是在一个区域(部门或子部门)中拥有多个运营商? 2)我不明白为什么链接的 SQL 是相关的。你想从查询中得到什么结果集(英文)?
标签: sql join database-design relational-database