Sam*_*ini 4 sql database oracle
我有一些必须测量的数据,这些数据不在任何表格中.我无法将其插入表中,也无法创建任何表并插入这些数据.所以我使用了以下两个来获取该表.我用它来加入其他表.
with movie_genre as
(
select '10' as "id", 'action' as "genre" from dual
union select '20' as "id", 'horror' as "genre" from dual
union select '30' as "id", 'comedy' as "genre" from dual
union select '40' as "id", 'adventure' as "genre" from dual
union select '50' as "id", 'drama' as "genre" from dual
union select '60' as "id", 'mystery' as "genre" from dual
union select '70' as "id", 'musical' as "genre" from dual
)
select * from movie_genre
;
Run Code Online (Sandbox Code Playgroud)
所以我得到了结果 -
id genre
10 action
20 horror
30 comedy
40 adventure
50 drama
60 mystery
70 musical
Run Code Online (Sandbox Code Playgroud)
我的问题是,有没有更好的方法来做到这一点?任何建议都将是一个救生员.
一个例子 -
让我们说我们有一张桌子 -
create table movies (
id number,
name varchar2(50),
genre_id number
);
insert into movies values (1, 'the hulk', 10);
insert into movies values (2, 'dumb and dumber', 30);
insert into movies values (3, 'frozen', 70);
Run Code Online (Sandbox Code Playgroud)
我们需要这样的结果 -
name genre is_in_genre
the hulk action yes
the hulk horror no
the hulk comedy no
the hulk adventure no
the hulk drama no
the hulk mystery no
the hulk musical no
dumb and dumber action no
dumb and dumber horror no
dumb and dumber comedy yes
dumb and dumber adventure no
dumb and dumber drama no
dumb and dumber mystery no
dumb and dumber musical no
frozen action no
frozen horror no
frozen comedy no
frozen adventure no
frozen drama no
frozen mystery no
frozen musical yes
Run Code Online (Sandbox Code Playgroud)
在这里,我们没有任何movie_genre表.
您可以按照您想要的顺序将类型作为字符串传递,并使用正则表达式生成movie_genre表.sql在这里小提琴
with movie_genre as
(
select level * 10 as id, regexp_substr(genre,'[^,]+',1,level) as genre
from
(
select ('action,horror,comedy,adventure,drama,mystery,musical')
as genre from dual
)
connect by level <=REGEXP_COUNT(genre,'[^,]+')
)
select * from movie_genre;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22040 次 |
最近记录: |