常用sql

本文介绍了 Oracle SQL 中的实用技巧,包括获取系统日期、设置日期格式、使用 IF 语句等,还展示了如何处理表数据,例如插入日期型数据、删除重复记录、创建临时表以及获取两表间的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.获取系统日期:
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

2.设置日期格式:
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

3.获取系统时间戳:
select systimestamp from dual;

4.sql语句的if语句:
decode:
产品放在哪个仓库
select product_id,decode(warehouse_id,1,'Southlake',
2,'San Francisco',
3,'New Jersey',
4,'Seattle',
'unknow'
) "Location of inventory" from inventories;

简单case:

select cust_last_name,case credit_limit when 100 then 'low'
when 5000 then 'high'
else 'medium' end
from customers;

复杂case:

select name,(case when score<60 then 'D'
when score>=60 and score score<70 then 'C'
when score>=70 and score score<80 then 'B'
when score>=80 then 'A'
else 'no score' end
) from score;

5.把表student2与student3的数据插入到表tar中
insert into tar(id,first_name,last_name,major)
(select id,first_name,last_name,major)
from student2 where rownum<=5
union all
select id,first_name,last_name,major)
from student3 where rownum<=5
)

6.如何插入日期型数据

insert into d values('mary',to_date('2009-09-07 16:47:00','yyyy-mm-dd hh24:mi:ss'));

7.删除表中的重复记录

delete from table student where rowid not in(select min(rowid) from student group by id)

8.创建一个临时表
create table tem_student2
as
select * from student2
where 1=0;

1=0表示创建的临时表tem_student2中没有数据

9.得到两个表中不相同的记录
方法1:
select id,major from source
where id not in(select id from target)
union
select id,major from target
where id not in(select id from source);

方法2:
select id,major from (
select id,major from source
union
select id,major from target
)
group by id,major
having count(*)=1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值