我有CAMPUS_IDs(NY1,PA1,VA1)和CLASS_IDs(大约10个班级) 所以我必须创建一个包含Campus_id和Class_id列的表,并指定在哪个校区中教授课程。问题是所有校区都会教授一些课程,其中一些课程只在一个校区内授课。如何设计那张桌子?我试图创建下表,但它很乱:
_____________________
| class_id |campus_id
---------------------
| math101 | NY1 |
---------------------
| pyt218 | PA1 |
---------------------
| C767 | VA1 |
---------------------
| pyt218 | PA1 |
_____________________
和带有class_id的campus_id是另一个表的外键。
答案 0 :(得分:1)
所以你想列出所有课程的校园?对于像
这样的输出class_id | campus_id
math101 | NY1, PA1
pyt218 | PA1, VA1, NY1
...
您可以使用Oracle 10g中提供的wm_concat函数:
with d as (
select 'NY1' as campus_id, 'math101' as class_id from dual union all
select 'PA1' as campus_id, 'math101' as class_id from dual union all
select 'VA1' as campus_id, 'pyt218' as class_id from dual union all
select 'PA1' as campus_id, 'pyt218' as class_id from dual union all
select 'PA1' as campus_id, 'class3' as class_id from dual union all
select 'VA1' as campus_id, 'class3' as class_id from dual union all
select 'VA1' as campus_id, 'math101' as class_id from dual union all
select 'NY1' as campus_id, 'class3' as class_id from dual
)
select class_id, wm_concat(campus_Id)
From d
group by class_id
在Oracle 11.2中,有一个新功能“listagg”可用于生成相同的结果:
with d as (
select 'NY1' as campus_id, 'math101' as class_id from dual union all
...
)
select class_id, listagg(campus_id, ',') within group (order by campus_id)
From d
group by class_id