首页 > 代码库 > oracle 数据库开发面试题,当时笔试的时候一个没做出来,现附原题及答案

oracle 数据库开发面试题,当时笔试的时候一个没做出来,现附原题及答案

1、

ID
1
2
3
5
6
7
8
10
11
12
15

表名tt,用sql找出ID列中不连续的ID,例如其中没有的4:

--创建表及数据CREATE TABLE tt(ID INTEGER);INSERT INTO tt SELECT 1 FROM dualUNION ALLSELECT 2 FROM dualUNION ALLSELECT 3 FROM dualUNION ALLSELECT 5 FROM dualUNION ALLSELECT 6 FROM dualUNION ALLSELECT 7 FROM dualUNION ALLSELECT 8 FROM dualUNION ALLSELECT 10 FROM dualUNION ALLSELECT 11 FROM dualUNION ALLSELECT 12 FROM dualUNION ALLSELECT 15 FROM dual;COMMIT;

 

--用到了connect by level 造数据WITH IT AS (SELECT LEVEL ID FROM DUAL CONNECT BY LEVEL <= (SELECT MAX(ID) FROM TT))SELECT A.ID  FROM IT A WHERE NOT EXISTS (SELECT 1 FROM TT B WHERE A.ID = B.ID)

 

2、

将录入不规范的房间信息整理成规范格式

不规范表(多个房间用逗号分割)

IDROOM
1101,102
2201,202,203
3301
....... 

 

 

 

 

 

规范表

IDROOM
1101
1102
2201
2202
2203
3301
...... 

 

 

 

 

 

 

 

 

--单行单列转多行--创建表及数据create table ttt(id integer,room varchar2(200));insert into tttselect 1,101,102 from dualunion allselect 2,201,202,203 from dualunion allselect 3,301 from dual;commit;
SELECT DISTINCT ID,REGEXP_SUBSTR(room, [^,]+, 1, LEVEL, i) AS STR    FROM tttCONNECT BY LEVEL <= LENGTH(room) - LENGTH(REGEXP_REPLACE(room, ,, ‘‘))+1;

 

oracle 数据库开发面试题,当时笔试的时候一个没做出来,现附原题及答案