1.生成uuid
sys_guid()函數
select sys_guid() from dual;
2.複製表數據到另外一張表
2.1複製表結構及數據
create table new_table as select * from old_table
2.2只複製表結構:
create table new_table as select * from old_table where 1<>1
2.3複製表的指定字段:
create table new_table as select column1,column2… from old_table where 1<>1
2.4複製表的指定字段及數據:
create table new_table as select column1,column2… from old_table where
2.5在已存在的表中插入數據
A.兩個表結構一樣
insert into new_table select * from old_table (前提是必須要有一個new_table 表才能查數據)
需要替換某些列的數據
insert into new_table (content,topicId) select REPLACE(content, 'aa', 'bb'),id FROM old_table
REPLACE(列,舊數據,新數據)
B.表結構不一樣:
insert into new_table (column1,column2…) select column1,column2… from old_table (注意:兩個表中的要複製的列數據類型和長度最好要一致,要注意長度大小問題)
3.聚合開窗函數 OVER(PARTITION BY FCITY) 當group by等使用出現限制可以考慮
SELECT FName, FCITY, FAGE, FSalary,COUNT(FName) OVER(PARTITION BY FCITY) FROM T_Person
OVER(PARTITION BY FCITY)表示對結果集按照FCITY進行分區,並且計算當前行所屬的組的聚合計算結果。在同一個SELECT語句中可以同時使用多個開窗函數,而且這些開窗函數並不會相互干擾。
4.格式轉換
to_date,to_char,to_number
4.1. 日期和字符轉換函數用法(to_date,to_char)
elect to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;
4.2.求某天是星期幾
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual; //中文
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; //英文
4.3 兩個日期間的天數
select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
4.4. Next_day的用法
Next_day(date, day)
4.5.找出今年的天數
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') FROM DUAL
4.6找出日期所在季度 返回數字1,2,3,4
select to_char(to_date('2019-06','yyyy-mm'),'q') from dual
5.case when函數的用法
select * from( select
CASE
WHEN TO_NUMBER(TO_CHAR(to_date('2019-05','yyyy-mm'), 'MM')) >= 7 THEN '2'
ELSE '1'
END AS half_year
from dual )t
where t.half_year = '2'
6.樹的遞歸查詢
SELECT m.col1 from tab1 m
start with col1 in(
SELECT n.col1
FROM tab2 n
)
connect by prior m.col1 = m.col1_father
7.in與exists使用對比
in 和exists
in是把外表和內表作hash 連接,而exists 是對外表作loop 循環,每次loop 循環再對內表進行查詢。
如果查詢的兩個表大小相當,那麼用in 和exists 差別不大。
如果兩個表中一個較小,一個是大表,則子查詢表大的用exists,子查詢表小的用in:
not in 和not exists
如果查詢語句使用了not in 那麼內外表都進行全表掃描,沒有用到索引;
而not extsts 的子查詢依然能用到表上的索引。所以無論那個表大,用not exists 都比not in 要快。