当前位置: 首页 > news >正文

进阶2 条件查询

目录

    • 〇. 概述
    • 一. 按条件表达式筛选
    • 二. 按逻辑表达式筛选
    • 三. 模糊查询
      • 1. like
      • 2. between and(可以提高语句的简洁度)
      • 3. in
      • 4. is null
      • 补充:安全等于<=>
        • is null   VS   <=>
    • 练习题

〇. 概述

  • 语法:select 查询列表 from 表名 where 筛选条件;
    先执行from 表名,再执行where 筛选条件,最后执行select 查询列表
  • 分类:
    • 一. 按条件表达式筛选
      条件运算符:> < = <>(!=) <= >=
    • 二. 按逻辑表达式筛选(逻辑运算符主要用于连接条件表达式)
      逻辑运算符:and(&&) or(||) not(!)
    • 三. 模糊查询
      like
      between and
      in
      is null
      is not null

一. 按条件表达式筛选

#案例1:查询工资大于12000的员工的信息 
select * from employees where salary >12000;
#案例2:查询部门编号不等于90号的员工姓名和部门编号 
select first_name,last_name,department_id from employees where department_id<>90;

二. 按逻辑表达式筛选

#案例1:查询工资在10000到20000之间的员工的员工名,工资以及奖金
select *from employees;
select first_name,last_name,salary, ifnull(commission_pct,0)*salary*12 from employees
	where salary>10000 and salary<20000;
#案例2:查询部门编号不是在90到110之间([90,110])的,或者工资高于15000的员工信息 
select * from employees where department_id<90 or department_id>110 or salary>15000;
select * from employees where not(department_id>=90 and department_id<=110) or salary>15000;

三. 模糊查询

  1. like
    ①一般和通配符搭配使用。
      通配符:
        % 任意多个字符,包含0个字符;
        _ 任意单个字符
      使用通配符时需要特别注意一下,通配符不能代表NULL
      如果需要匹配NULL的话,需要使用IS NULL或者IS NOT NULL!
  2. between and 完全等价于<= >=
    ①可以提高语句的简洁度
    ②包含临界值
    ③两个临界值顺序不可以颠倒
  3. in 等价于=,不是like
    含义:用于判断某字段的值是否属于in列表中的某一项
    ①使用in提高语句简洁度
    ②in列表中的值类型必须统一或者兼容(可以隐式转换)
    ③in列表中的值不支持通配符(因为关键字in相当于=,而不是like)
  4. is null
    =或<>不能用于判断null值
    is null或is not null可以判断null值

1. like

#案例1;查询员工名中包含字母a的员工信息(筛选条件不具体,模糊匹配)
select * from employees where first_name like '%a%';#%代表通配符,可以是任意一种字符(串)
#案例2:查询员工名中第三个字符为e,第五个字符为a的员工的员工名和工资
select first_name,salary from employees where first_name like '__e_a%';
#案例3:查询员工姓中第二个字符为_的员工的姓(使用转义字符\)
select last_name from employees where last_name like '_\_%';
#转移字符可以自己指定,使用关键字escape(设计这种用法的原因是查询条件中可能会用到转移字符本身代表的字符)
select last_name from employees where last_name like '_$_%' escape '$';
#案例4:查询部门编号大于等于100的员工的信息 
select * from employees where department_id like '1__';

2. between and(可以提高语句的简洁度)

#案例1:查询员工编号在100到120之间([100,120])的所有员工的信息 
select * from employees where employee_id>=100 and employee_id<=120;
select * from employees where employee_id between 100 and 120;

3. in

#案例1:查询员工的工种编号是IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号 
select first_name,job_id from employees 
	where job_id='IT_PROG' or job_id='AD_VP' or job_id='AD_PRES';
select first_name,job_id from employees where job_id in('IT_PROG','AD_VP','AD_PRES');

4. is null

#案例1:查询没有奖金的员工的名和奖金率 
select first_name,commission_pct from employees where commission_pct=null;#错误,=运算符不能判断NULL值
select first_name,commission_pct from employees where commission_pct is null;

补充:安全等于<=>

(可以判断null值,也可以判断其他值)

#案例1:查询没有奖金的员工的名和奖金率
select first_name,commission_pct from employees where commission_pct <=> null;
#案例2:查询工资为12000的员工的信息 
select * from employees where salary <=> 12000;

is null   VS   <=>

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

练习题

#1:查询员工号为176的员工姓名和部门号和年薪 
select first_name,last_name,department_id,salary*12*(1+ifnull(commission_pct,0))
	as 年薪 from employees where employee_id=176;
#2. 查询没有奖金,且工资小于18000的员工的salary,last_name
select salary,last_name from employees where commission_pct is null and salary<18000;
#3. 查询job_id不为'IT_PROG'或者工资为12000的员工的信息 
select * from employees where job_id <> 'IT_PROG' or salary=12000;
#4. 查看部门departments表的结构 
desc departments;
#5. 查询部门departments表中涉及到了哪些位置编号 
select distinct location_id from departments;
select * from employees where commission_pct like '%%' and last_name like '%%';

相关文章:

  • Swift 周报 第二十一期
  • 酒店预订订单的分析与建模【决策树、xgboost】
  • 小程序项目学习--第六章:项目实战二、推荐歌曲-歌单展示-巅峰榜-歌单详情-页面优化
  • MongoDB ObjectId
  • 分布式锁原理及Redis如何实现分布式锁
  • buctoj-2023寒假集训-进阶训练赛(十六)
  • 辨别三星内存条的真假
  • 数据结构 - 树和二叉树
  • 【C++之类和对象】初识类和对象
  • React(一) —— 组件的创建与state
  • mysql-视图的定义和简单使用
  • PTA L1-027 出租(详解)
  • XXE漏洞常见利用点总结
  • 让你深夜emo的“网抑云”,是如何做产品设计的?
  • Codeforces Round #848 (Div. 2) D - Flexible String Revisit
  • 「题解」字符串中的所有单词进行倒排
  • 关于符合车规的高精度定位产品
  • 【Linux】基础网络编程
  • torch_geometric--Convolutional Layers
  • Java——OpenJDK和JDK的区别
  • 电加热油锅炉工作原理_电加热导油
  • 大型电蒸汽锅炉_工业电阻炉
  • 燃气蒸汽锅炉的分类_大连生物质蒸汽锅炉
  • 天津市维修锅炉_锅炉汽化处理方法
  • 蒸汽汽锅炉厂家_延安锅炉厂家
  • 山西热水锅炉厂家_酒店热水 锅炉
  • 蒸汽锅炉生产厂家_燃油蒸汽发生器
  • 燃煤锅炉烧热水_张家口 淘汰取缔燃煤锅炉
  • 生物质锅炉_炉
  • 锅炉天然气_天燃气热风炉