Mysql 查询案例--基础查询100道+进阶查询100道

– 基础查询案例

    /*sql语句学习*/
     
    /*
    SELECT [DISTINCT] {*, column alias,..}  
    FROM table alias 
    Where 条件表达式
    */
    -- 查询EMP表中部门标号是10的员工
    select empno, ename, job from emp where deptno = 10;
    -- distinct 去除重复数据
    select distinct deptno from emp;
    -- 去重也可以针对多个字段,多个字段值只要有一个不匹配就算是不同的记录
    select distinct deptno,sal from emp;
    -- 查询过程中可以给列添加别名,也可以给表添加别名
    select e.empno 雇员编号,e.ename 雇员名称, e.job 雇员工作 from emp e where e.deptno=10;
    -- 给列起别名,可以加as,也可以不加,看心情
    select e.empno as 雇员编号,e.ename as 雇员名称, e.job as 雇员工作 from emp e where e.deptno=10;
    -- 给列起别名,如果别名中包含空格,那么需要将别名用""包含起来
    select e.empno as "雇员 编号",e.ename as "雇员 名称", e.job as "雇员 工作" from emp e where e.deptno=10;
    -- 查询表中的所有字段,可以使用*,但是项目中千万不要随便使用*,容易被打死
    select * from emp;
     
    /*
     =,!=,<>,<,>,<=,>=,any,some,all 
     is null,is not null 
     between x and y 
     in(list),not in(list) 
     exists(sub-query) 
     like  _ ,%,escape ‘\‘   _\% escape ‘\’ 
    */
    -- =
    select * from emp where deptno = 20;
    -- !=
    select * from emp where deptno != 20;
    -- <> 不等于
    select * from emp where deptno <> 20;
    -- <
    select sal from emp where sal < 1500;
    -- >
    select sal from emp where sal < 1500;
    -- <=
    select sal from emp where sal <= 1500;
    -- >=
    select sal from emp where sal >= 1500;
    -- 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(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 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值