在一个查询中选择不同的子类型值
Selecting different subtype values in one query
我是 oracle 和处理子类型的新手。我需要 select 一张 MP3_TYPE 和 DISK_TYPE 中 MEDIA_TYPE 两个值的专辑:Vinyl,Audio CD。
这是创建子类型查询:
create or replace type disk_type under album_type
( mediaType varchar(10),
diskNum number(2), -- number of disks
diskUsedPrice number(9,2),
diskDeliveryCost number(9,2),
overriding member function discountPrice return number)
/
create or replace type mp3_type under album_type
(downloadSize number, -- size in MB
overriding member function discountPrice return number)
/
我可以单独 select 所有 3 种类型,但是当所有 3 种都在一个查询中时它似乎不起作用。治疗功能不太好,所以假设这是一些语法问题。
这是我所做的:
select
t1.albumtitle
from albums t1
where value(t1) IS OF (mp3_type)
and treat(value(t1) as disk_type).mediatype = 'Vinyl'
and treat(value(t1) as disk_type).mediatype = 'Audio CD';
有什么想法吗?
在您目前显示的代码中,mp3_type
和 disk_type
都是 album_type
的子类型。这似乎是合理的。但这意味着 table 中的单个对象不能同时是两个子类型;所以它不能同时具有两个子类型的属性。
如果某行是 mp3_type
,则将其视为 disk_type
不会导致设置 disk_type
属性。当您将该对象视为 disk_type
时,这些属性将为 - 必须 为 - null。
insert into albums values (album_type('ABC'));
insert into albums values (mp3_type('BCD', 123));
insert into albums values (disk_type('DEF', 'Vinyl', 1, 12.34, 1.23));
select
t1.albumtitle
from albums t1
where value(t1) IS OF (mp3_type)
and treat(value(t1) as disk_type).mediatype = 'Vinyl'
and treat(value(t1) as disk_type).mediatype = 'Audio CD';
no rows selected
select
t1.albumtitle,
treat(value(t1) as disk_type).mediatype
from albums t1
where value(t1) IS OF (mp3_type);
ALBUMTITLE TREAT(VALU
------------------------------ ----------
BCD
I need to select an album that is of MP3_TYPE and both values of MEDIA_TYPE in DISK_TYPE: Vinyl, Audio CD
如果是MP3_TYPE则MEDIA类型为null;但即使使用 disk_type
条目,媒体类型也不能同时是 both Vinyl 和 CD。
也许您只是想说或使用 or
而不是 and
:
select
t1.albumtitle,
treat(value(t1) as mp3_type).downloadsize as downloadsize,
treat(value(t1) as disk_type).mediatype as mediatype
from albums t1
where
value(t1) IS OF (mp3_type)
or (
value(t1) IS OF (disk_type)
and (
treat(value(t1) as disk_type).mediatype = 'Vinyl'
or treat(value(t1) as disk_type).mediatype = 'Audio CD'
)
);
ALBUMTITLE DOWNLOADSIZE MEDIATYPE
------------------------------ ------------ ----------
BCD 123
DEF Vinyl
我是 oracle 和处理子类型的新手。我需要 select 一张 MP3_TYPE 和 DISK_TYPE 中 MEDIA_TYPE 两个值的专辑:Vinyl,Audio CD。
这是创建子类型查询:
create or replace type disk_type under album_type
( mediaType varchar(10),
diskNum number(2), -- number of disks
diskUsedPrice number(9,2),
diskDeliveryCost number(9,2),
overriding member function discountPrice return number)
/
create or replace type mp3_type under album_type
(downloadSize number, -- size in MB
overriding member function discountPrice return number)
/
我可以单独 select 所有 3 种类型,但是当所有 3 种都在一个查询中时它似乎不起作用。治疗功能不太好,所以假设这是一些语法问题。
这是我所做的:
select
t1.albumtitle
from albums t1
where value(t1) IS OF (mp3_type)
and treat(value(t1) as disk_type).mediatype = 'Vinyl'
and treat(value(t1) as disk_type).mediatype = 'Audio CD';
有什么想法吗?
在您目前显示的代码中,mp3_type
和 disk_type
都是 album_type
的子类型。这似乎是合理的。但这意味着 table 中的单个对象不能同时是两个子类型;所以它不能同时具有两个子类型的属性。
如果某行是 mp3_type
,则将其视为 disk_type
不会导致设置 disk_type
属性。当您将该对象视为 disk_type
时,这些属性将为 - 必须 为 - null。
insert into albums values (album_type('ABC'));
insert into albums values (mp3_type('BCD', 123));
insert into albums values (disk_type('DEF', 'Vinyl', 1, 12.34, 1.23));
select
t1.albumtitle
from albums t1
where value(t1) IS OF (mp3_type)
and treat(value(t1) as disk_type).mediatype = 'Vinyl'
and treat(value(t1) as disk_type).mediatype = 'Audio CD';
no rows selected
select
t1.albumtitle,
treat(value(t1) as disk_type).mediatype
from albums t1
where value(t1) IS OF (mp3_type);
ALBUMTITLE TREAT(VALU
------------------------------ ----------
BCD
I need to select an album that is of MP3_TYPE and both values of MEDIA_TYPE in DISK_TYPE: Vinyl, Audio CD
如果是MP3_TYPE则MEDIA类型为null;但即使使用 disk_type
条目,媒体类型也不能同时是 both Vinyl 和 CD。
也许您只是想说或使用 or
而不是 and
:
select
t1.albumtitle,
treat(value(t1) as mp3_type).downloadsize as downloadsize,
treat(value(t1) as disk_type).mediatype as mediatype
from albums t1
where
value(t1) IS OF (mp3_type)
or (
value(t1) IS OF (disk_type)
and (
treat(value(t1) as disk_type).mediatype = 'Vinyl'
or treat(value(t1) as disk_type).mediatype = 'Audio CD'
)
);
ALBUMTITLE DOWNLOADSIZE MEDIATYPE
------------------------------ ------------ ----------
BCD 123
DEF Vinyl