Oracle语句积累

  1. 以别的表中字段(包含数据)为基础创建新表:
    create table t2 as select t1.a,t1.b from t1;
  2. 用一张表中的数据更新另一张表的数据:
    update t1 set t1.a = (select t2.a from t2 where t1.b = t2.b);
  3. 使用转义字符查询, ESCAPE 将任意符号设为转义字符,其后紧邻的 '_' 不是模糊匹配:
    select * from t1 where table_name like 'abc_z/_%' ESCAPE '/';
  4. 特殊字符转义+模糊查询:
    select ASCII('_') from dual;  --先查出编码
    select 1 from t where t.a like '%'||'char(45)'||'%'; --拼接模糊查询
  5. 将一张表中数据插入到另一张表,括号中可加相应字段,也可全字段插入,注意 select 后不加括号,否则语法错误:
    insert into t1(t1.a,t1.b) select t2.a,t2.b from t2;
    insert into t1 select * from t2;
  6. 转换日期格式,其中 localTimeStamp 格式为"01-8月-50 08.00.00.0000 上午",+0为Oracle自带方法:
    select cast(cast(localTimeStamp as timeStamp) as date) from dual;
    select cast(localTimeStamp as timeStamp)+0 from dual;
  7. 查找某字段名,注意条件字段名、表名要大写,否则空集:
    select * from USER_TAB_COLUMNS where table_name = 'T1' and colum_name like '%A%';
  8. 剔除字段中非数字类型数据,保留数字:
    select translate(a,'0'||translate(a,'#0123456789-.','#'),'0') a from t1;
  9. 截取字符串函数:
    substr(string, start, [length])
    查找字符串位置:
    instr(string, substring, strat, appear_position)
    从左边添加字符串:
    lpad(string, padded_length, [pad_string])
  10. 判断字段中是否含有中文:
    select 1 from t where ASCIISTR(t.a) like '%\%';
  11. trim()方法只能去头尾空格,中间空格用replace()方法替换成 ''
  12. 删除表中多余的重复记录,条件为单个字段ID,只保留rowid最小的记录:
    delete from t1 where (id) in 
    	(select id from t1 group by id having count(id)>1) 
    	and rowid not in 
    	(select MIN(rowid) from t1 group by id having count(1)>1);
  13. 查找表中索引:
    select * from USER_IND_COLUMNS t where t.table_name;

猜你喜欢

转载自blog.csdn.net/qq_35221523/article/details/79984834