oracle-查漏补缺-某一些关键字

复杂一点的另外的博客记录下来了,比如rownum,start with connect by prior,关联查询

--order by 默认升序asc,desc是降序
select id, img_name from imgs order by id desc; 
--+ 数字,是把字符转成number, 然后加上这个数字, 然后在转成字符,如果这个字符不是数字格式,则会报错
select id + 100, img_name from imgs ;
select id , img_name + 100 from imgs order by id desc;
--case when then when then else end
select case a.id when '1000000006' then '男' when '1000000009' then '女' else 'weizhi' end sex, a.img_name from imgs a;
--between and  等效  >= and <=
select * from development_application_group where group_numberforpeoples between 9 and 19;
select * from development_application_group where group_id between '1' and '4';
--all,any
--all是对比的所有数据和我们准备比较的数据,都满足条件,这条数据才不会被过滤,>那就要大于所有,<就要小于所有;any不是所有,有一个符合条件就好

alter table boys add(salary number default 10000);
select * from girls;
select * from boys for update;
select * from boys where salary < any(select salary from girls);
select * from boys where salary > all(select salary from girls);
--union 和 union all,有点类似并集
--两个sql查询的字段,和顺序都必须一样,此时我们就可以把两者合并起来一起查询,避免两次sql查询,浪费资源,
--区别在于如果有某两行数据是一样的union则只显示一条,union all则会全部显示出来

insert into boys (id, name, salary) values ('g_6', '闭月羞花', '3000');
insert into girls (id, name, salary) values ('g_6', '闭月羞花', '3000');
select id, name, salary from boys union select id, name, salary from girls;
select id, name, salary from boys union all select id, name, salary from girls;
--intersect,类似交集,多条重复的也只取一条
select id, name, salary from boys intersect select id, name, salary from girls;
--minus,显示的结果是针对的第一个sql的数据,如果有存在于第二条sql的数据,则这条数据则不会显示
select id, name, salary from boys minus select id, name, salary from girls;                                                                                   

--in, not in
select * from girls where id in('g_1', 'g_2');
select * from girls where (id, name, salary) in (select 'g_1', '张雨绮', '10000' from dual);                                                                

--exists, not exists
--exists可以理解为对第一个select查询出来的数据进行过滤,过滤的方式就是把对应的数据拿到exists里面的select去查询一次,能查到数据就不过滤,not exists相反

--下面这个例子就是先是查询出所有女生,然后只显示已经找到男朋友的女生
select * from girls a where exists (select 1 from boys b where a.id = b.gf_id);

--is not, is not null很简单没啥说的
--like 模糊查询,%表示0个或多个,_表示一个,也没好说的

猜你喜欢

转载自blog.csdn.net/blossomfzq/article/details/82895259