数据库sql语句的一些简单记录,包括exists,模糊查询,nvl,order by,交、并、全、差集

  • -any,取其中任意一个
    select sal from emp where sal > any(1000,1500,3000);

  • -some,some跟any是同一个效果,只要大于其中某一个值都会成立
    select sal from emp where sal > some(1000,1500,3000);

  • -all,大于所有的值才会成立
    select sal from emp where sal > all(1000,1500,3000);

  • -is null,在sql的语法中,null表示一个特殊的含义,null != null,不能使用=,!=判断,需要使用is ,is not
    select * from emp where comm is null;

  • -,is not null
    select * from emp where comm is not null;
    select * from emp where null is null;

  • -between x and y,包含x和y的值
    select * from emp where sal between 1500 and 3000;
    select * from emp where sal >=1500 and sal <=3000;
    –需要进行某些值的等值判断的时候可以使用in和not in

  • -in(list),
    select * from emp where deptno in(10,20);

  • -可是用and 和or这样的关键字,and相当于是与操作,or相当于是或操作
    and和or可能出现在同一个sql语句中,此时需要注意and和or的优先级
    –and 的优先级要高于or,所以一定要将or的相关操作用()括起来,提高优先级
    select * from emp where deptno = 10 or deptno = 20;

  • -not in(list)
    select * from emp where deptno not in(10,20);
    select * from emp where deptno != 10 and deptno !=20;

  • -exists :exists(sub-query),当exists中的子查询语句能查到对应结果的时候,意味着条件满足
    相当于双层for循环
    –现在要查询部门编号为10和20的员工,要求使用exists实现
    select * from emp where deptno = 10 or deptno = 20;
    –通过外层循环来规范内层循环
    select *
    from emp e
    where exists (select deptno
    from dept d
    where (d.deptno = 10 or d.deptno = 20)
    and e.deptno = d.deptno)

/*
模糊查询:
like  _ ,%,escape ‘\‘   _\% escape ‘\’
在like的语句中,需要使用占位符或者通配符
_,某个字符或者数字仅出现一次
%,任意字符出现任意次数
escape,使用转义字符,可以自己规定转义字符
使用like的时候要慎重,因为like的效率比较低
使用like可以参考使用索引,但是要求不能以%开头
涉及到大文本的检索的时候,可以使用某些框架 luence,solr,elastic search
*/
--查询名字以S开头的用户
select * from emp where ename like('S%')
--查询名字以S开头且倒数第二个字符为T的用户
select * from emp where ename like('S%T_');
select * from emp where ename like('S%T%');
--查询名字中带%的用户
select * from emp where ename like('%\%%') escape('\')
order by进行排序操作
默认情况下完成的是升序的操作,
asc:是默认的排序方式,表示升序
desc:降序的排序方式
排序是按照自然顺序进行排序的
如果是数值,那么按照从大到小
如果是字符串,那么按照字典序排序
在进行排序的时候可以指定多个字段,而且多个字段可以使用不同的排序方式
每次在执行order by的时候相当于是做了全排序,思考全排序的效率
会比较耗费系统的资源,因此选择在业务不太繁忙的时候进行
*/
select * from emp order by sal;
select * from emp order by sal desc;
select * from emp order by ename;
select * from emp order by sal desc,ename asc;
--使用计算字段
--字符串连接符
select 'my name is '||ename name from emp;
select concat('my name is ',ename) from emp;
--计算所有员工的年薪
select ename,(e.sal+e.comm)*12 from emp e;
--null是比较特殊的存在,null做任何运算都还是为null,因此要将空进行转换
--引入函数nvl,nvl(arg1,arg2),如果arg1是空,那么返回arg2,如果不是空,则返回原来的值
select ename,(e.sal+nvl(e.comm,0))*12  from emp e;
--A
select * from emp where deptno =30;
--B
select * from emp where sal >1000; 
--并集,将两个集合中的所有数据都进行显示,但是不包含重复的数据
select * from emp where deptno =30 union
select * from emp where sal >1000;
--全集,将两个集合的数据全部显示,不会完成去重的操作
select * from emp where deptno =30 union all
select * from emp where sal >1000;
--交集,两个集合中交叉的数据集,只显示一次
select * from emp where deptno =30 intersect 
select * from emp where sal >1000;
--差集,包含在A集合而不包含在B集合中的数据,跟A和B的集合顺序相关
select * from emp where deptno =30 minus 
select * from emp where sal >1000;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值