• 第六节--条件查询


    根据筛选条件的写法不同,分为以下几大类:

    一、按条件表达式筛选

    条件运算符:>    <     =    <> 或!=   >=    <=

    例:查询工资>12000的员工信息

    select   *  from employees where salary>12000;

    二、按逻辑表达式筛选

    逻辑运算符:and(&&)       or(II)      not(!), 在MySQL中标准的为: and  ,or , not

    例:1.查询工资在1万到2万之间的员工名,工资以及奖金

    select last_name, salary, commission_pct from employees where salary>=10000 and salary <=20000;

            2. 查询部门编号不是90到110之间或者工资高于15000的员工信息。

    select  * from employees where department_id<90 or department_id>100 or salary>15000;

    三、模糊查询

    ① like 

    特点:一般和通配符搭配使用{  %任意多个字符,包含0个字符;_任意单个字符}

    例1:查询员工名中包含字符a的员工信息。

    select * from employees where last_name like '%a%';

    例2:查询员工名中第三个字符为E第五个字符为a的员工名和工资。

    select last_name,salary from employees where last_name like '__e_a%';

    例3. 查询员工名中第二个字符为_的员工名

    select last_name  from employees where last_name like '_\_%;

    此段代码中 "" 是作为转译符,转译通配符 _ ,将该通配符转译为普通字符;

    ②between and

    例:查询员工编号100到120之间的员工信息

    select * from employees where employee_id between 100 and 120;

    between and 的注意事项:

    ①可以调高简洁度

    ②包含临界值

    ③临界值不要调换顺序

    ③.  in ----判断某字段的值是否属于in列表的某一项

    例:查询员工的工种编号IT_PROG/AD_VP/AD_PRES中的一个员工名的工种编号

    select last_name, job_id from employees where job_id in ('IT-PROG','AD_VP','AD_PRES');

    特点:①使用IN提高语句简洁度

               ②IN 列表值类型必需统一货兼容

    ④. Is null

    例:查询没有奖金的员工名和奖金率

    select last_name, commission_pct from employees where commission_pct is null;

    '='    或    ‘<>’不能用于判断 nul l值

    is null 或 is not null 可以判断 null 值

    ⑤.  安全等于<=>

    例:1. 查询没有奖金的员工名和奖金率

    select last_name, commission_pct from employees where commission_pct <=>null;

    例:2. 查询工资为12000的员工信息

    select last_name,salary from employees where salary<=>12000;

    ⑥ is null 与 <=>

    is null 仅仅可以判断null值,可读性较高建议使用,<=>既可以判断null值,又可以判断普通的数值,可读性较低。

    例:查询员工号176的员工的姓名和部门号和年薪

    select last_name, department_id, salary*12*(1+ifnull(commission,0) as 年薪 from employees where employee_id=176;
    Jasminelee
  • 相关阅读:
    053(二十五)
    053(二十四)
    053(二十三)
    053(二十二)
    053(二十一)
    053(二十)
    053(十九)
    053(十八)
    053(十七)
    单例设计模式
  • 原文地址:https://www.cnblogs.com/Jasmine6-Lee/p/12620550.html
Copyright © 2020-2023  润新知