select 嵌套 table 类型的一行
select one row from nested table type
我的问题是 select 只有该列类型的一条记录作为默认值。
create type t_tel as table of number;
create table users_tel(
user_id number,
user_name varchar2(100),
tel t_tel
) nested table tel store as tel_table;
insert into users_tel(user_id, user_name, tel) values (1, 'Amir', t_tel(987,654,321));
select * from users_tel;
使用 table 集合表达式将嵌套 table 中的集合视为 table 并加入它。然后你可以过滤得到每行 user_id
:
Oracle 11g R2 架构设置:
create type t_tel as table of number;
create table users_tel(
user_id number,
user_name varchar2(100),
tel t_tel
) nested table tel store as tel_table;
insert into users_tel(user_id, user_name, tel)
SELECT 1, 'Amir', t_tel(987,654,321) FROM DUAL UNION ALL
SELECT 2, 'Dave', t_tel(123,456) FROM DUAL UNION ALL
SELECT 3, 'Kevin', t_tel() FROM DUAL;
查询 1:
SELECT user_id,
user_name,
tel_no
FROM (
SELECT u.*,
t.column_value AS tel_no,
ROW_NUMBER() OVER ( PARTITION BY u.user_id ORDER BY ROWNUM ) AS rn
FROM users_tel u
LEFT OUTER JOIN
TABLE( u.tel ) t
ON ( 1 = 1 )
)
WHERE rn = 1
| USER_ID | USER_NAME | TEL_NO |
|---------|-----------|--------|
| 1 | Amir | 987 |
| 2 | Dave | 123 |
| 3 | Kevin | (null) |
你可以使用group by来完成:
select u.user_id, u.user_name, min(t.column_value) def_tel
from users_tel u
left join table(u.tel) t on 1=1
group by u.user_id, u.user_name;
请注意,我使用 left join 和 table 类型来显示 tel
为空的记录。
我的问题是 select 只有该列类型的一条记录作为默认值。
create type t_tel as table of number;
create table users_tel(
user_id number,
user_name varchar2(100),
tel t_tel
) nested table tel store as tel_table;
insert into users_tel(user_id, user_name, tel) values (1, 'Amir', t_tel(987,654,321));
select * from users_tel;
使用 table 集合表达式将嵌套 table 中的集合视为 table 并加入它。然后你可以过滤得到每行 user_id
:
Oracle 11g R2 架构设置:
create type t_tel as table of number;
create table users_tel(
user_id number,
user_name varchar2(100),
tel t_tel
) nested table tel store as tel_table;
insert into users_tel(user_id, user_name, tel)
SELECT 1, 'Amir', t_tel(987,654,321) FROM DUAL UNION ALL
SELECT 2, 'Dave', t_tel(123,456) FROM DUAL UNION ALL
SELECT 3, 'Kevin', t_tel() FROM DUAL;
查询 1:
SELECT user_id,
user_name,
tel_no
FROM (
SELECT u.*,
t.column_value AS tel_no,
ROW_NUMBER() OVER ( PARTITION BY u.user_id ORDER BY ROWNUM ) AS rn
FROM users_tel u
LEFT OUTER JOIN
TABLE( u.tel ) t
ON ( 1 = 1 )
)
WHERE rn = 1
| USER_ID | USER_NAME | TEL_NO |
|---------|-----------|--------|
| 1 | Amir | 987 |
| 2 | Dave | 123 |
| 3 | Kevin | (null) |
你可以使用group by来完成:
select u.user_id, u.user_name, min(t.column_value) def_tel
from users_tel u
left join table(u.tel) t on 1=1
group by u.user_id, u.user_name;
请注意,我使用 left join 和 table 类型来显示 tel
为空的记录。