数据库学习(一)

  工作中是由数据库研发人员管理数据库、表、数据等,作为测试,我主要还是在测试中遇到需要核对数据库表数据时才有用到,所以我把平时用到的一些SQL语句整理了一下,以便学习。我们用到的主要是Oracle数据库,使用的工具是PL/SQL。  

  PL/SQL Developer是一个集成开发环境,专门开发面向Oracle数据库的应用。PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL)。PL/SQL是Oracle数据库对SQL语句的扩展。在普通SQL语句的使用上增加了编程语言的特点,所以PL/SQL把数据操作和查询语句组织在PL/SQL代码的过程性单元中,通过逻辑判断、循环等操作实现复杂的功能或者计算。PL/SQL 只有 Oracle 数据库有。 MySQL 目前不支持 PL/SQL 的,但支持Navicat Premium。

——百度百科

1、简单的查询语句

select * from prod_V1.Fetch_Apply ;  --查询表中的所有数据

select * from prod_V1.Fetch_Apply t where t.applycode='SJTK20181206000023';   --where 表示条件;t表示表的别称,使用表名时就可以直接简写t就行

select * from prod_V1.Fetch_Apply t where t.applycode='SJTK20181206000023' for update;  --for update 执行后表示可以修改表

2、复杂一点的查询语句

select * from order_v1.statement t where t.order_id =(select c.orderid from order_v1.orders c where c.orderno= 17125371417);

--先在orders表中查询出要求的orderid,然后再查询出statement 表中相同的orderid的数据,这里要求orders表中查询的orderid是一条数据

select * from order_v1.statement t where t.order_id in(select c.orderid from order_v1.orders c where c.orderno= 17125371417);

--先在orders表中查询出要求的orderid,然后再查询出statement 表中相同的orderid的数据

in 和 = 的区别,就是前者子查询可以多条结果,后者子查询只能一条结果(否则会报错)

select * from order_v1.statement t where t.order_id not in(select c.orderid from order_v1.orders c where c.orderno= 17125371417);

 --not in 就是和in相反的,不存在的意思

select * from order_v1.statement t where exists(select c.orderid from order_v1.orders c where c.orderno= 17125371417 and c.orderid= t.order_id);

--exists 存在,和in类似,主查询(表statement )中的结果同时存在子查询(表orders )的结果中的数据

in和exists的区别:

  in 是先执行子查询得出结果,然后遍历主查询与结果对比,相同的就留下 ;exists 是先执行主查询,然后根据结果再执行子查询,得到结果

  执行顺序不同会影响效率,子查询结果集多就用exists,主查询结果集多就用in

资料参考:https://www.cnblogs.com/liyasong/p/sql_in_exists.html

https://www.cnblogs.com/clarke157/p/7912871.html

3、inner join 、 left join 、 right join 、full join的使用

select a.* from order_v1.Settle_Allot_Money a inner join order_v1.statement b on a.statement_id=b.id
where b.order_id = (
select c.orderid from order_v1.orders c where c.orderno=17122214222);

select * from order_v1.statement_notification r inner join (
select * from order_v1.statement a where a.order_id =(
select c.orderid from order_v1.orders c where c.orderno=17124893162 )
) s on r.uniquekey=s.unique_key ;

--写了三个表有点复杂,不过就是想说明下关联表里面可以条件查询,使用 join 就是不用分多次查不同表

内连接:inner join   表示把AB表的记录相符都显示出来,把AB表不符合条件的都排除

外连接分三种,即左连接(LEFT OUTER JOIN)、右连接(RIGHT OUTER JOIN)、全连接(FULL OUTER JOIN):

1.A LEFT OUTER JOIN B表示把A表的记录都显示出来,把B表符合条件的结果集显示出来,不符合条件的用NULL表示

2.A RIGHT OUTER JOIN B表示把B表的记录都显示出来,把A表符合条件的结果集显示出来,不符合条件的用NULL表示

3.A FULL OUTER JOIN B 表示把A表和B表的记录都显示出来,不符合条件的用NULL表示

资料参考:https://www.cnblogs.com/Impulse/p/5045872.html

4、时间

数据库表中的时间,会有不同的格式,查询时间段的数据时可以用 to_date函数编写时间

select * from settle_v1.platform_estimate_earnings_log t where t.amount > 0 and t.trans_type=4
and
t.log_time < to_date('2018-12-01','yyyy-mm-dd')
and
t.log_time >= to_date('2018-11-01','yyyy-mm-dd'); --查询的是11月份的数据

select sum(t.invocie_amount) from finc_v1.re_transaction_detail_day t where t.supplierid=1 and t.REFUND_TYPE=2
and
t.REFUND_CREATEDTIME < to_date('2018-8-30','yyyy-mm-dd')
and
t.REFUND_CREATEDTIME > to_date('2018-08-28 23:59:59','yyyy-mm-dd hh24:mi:ss');  --查询的是8.29号一天的数据

--注意 = 的使用,防止遗漏

5、排序

select * from order_v1.orders t order by t.orderid;
select * from order_v1.orders t order by t.orderid desc;

--order by 会自动排序(顺序),加上desc会重新排序(倒序)

猜你喜欢

转载自www.cnblogs.com/xiaoyu2018/p/10213372.html