oracle常用查询

databaseoracle

# oracle常用查询

user_tab_comments 表名备注表

user_col_comments 表字段备注表

# 指定表获取表名注释

select * from user_tab_comments where table_name='TABLE_NAME' and owner = 'OWNER_NAME';
1

# 指定表字段信息

-- select * from all_col_comments where owner = 'OWNER_NAME' AND table_name='TABLE_NAME' ;
select * from all_col_comments where owner = 'GXB2B2C' AND table_name='T_BOSS_ORG' ;

1
2
3

# 根据表名生成DDL字段注释

将生成的sql,多引号替换为单引号

select    concat('comment on column ',table_name,'.',column_name,' is ',CONCAT('"',comments,'";')) from user_col_comments where table_name = '表名' AND owner='库名'

--oracle种 concat函数不支持超过两个参数,所以使用 || 拼接查询
select 'comment on column ' || table_name || '.'|| column_name || ' is "' || comments || ' ";' from user_col_comments where table_name = '表名' ;

1
2
3
4
5

# 行转列查询

# 原数据

id key value
1 电脑 $1600
1 手机 $12
1 导管 $1
2 电脑 $2
2 手机 $22

# 方式一:MAX()

# 效果:

id 电脑 手机 导管
1 $1600 $12 $1
2 $2 $22

# SQL:


SELECT id,
	MAX(CASE WHEN type = '电脑' THEN VALUE END) 电脑,
	MAX(CASE WHEN type = '手机' THEN VALUE END) 手机,
	MAX(CASE WHEN type = '导管' THEN VALUE END) 导管 
FROM t_order
GROUP BY id
1
2
3
4
5
6
7

此方法没数据库限制

# 方式二:LISTAGG()

# 效果:

id type
1 电脑,手机,导管

# SQL:

SELECT id,LISTAGG(type,',') type FROM t_order 
WHERE id = 1;
1
2

此方法将,列数据整合到一个结果中

# 方式三:WM_CONCAT()

效果和方式二相同,但只适用于Oracle数据库,高版本Oracle不支持该函数;

# SQL:

SELECT id,WM_CONCAT(type,',') type 
FROM t_order 
WHERE id = 1;
1
2
3

# 列转多行查询

# 原数据

id month
1 1月,2月,5月
2 6月,12月

# 方式一:REGEXP_SUBSTR()

# 效果:

id month
1 1月
1 2月
1 5月
2 6月
2 12月

# SQL:

SELECT
    distinct
	t1.id,
	REGEXP_SUBSTR(t1.month, '[^,]+', 1, LEVEL) AS month
FROM
	t_test t1
CONNECT BY
	LEVEL <= regexp_count(t1.month,',' ) + 1
ORDER BY t1.id;
1
2
3
4
5
6
7
8
9
更新时间: 6 个月前