Mysql查询-条件查询

mysql_basic

Mysql查询-条件查询

语法

1
2
3
4
5
6
select 
查询列表
from
表名
where
筛选条件;

分类

条件表达式查询

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

案例一:查询工资大于12000的员工信息

1
2
3
4
5
6
select 
*
from
employees
where
salary > 12000;

案例二:查询部门编号不等于90的员工姓名和部门编号

1
2
3
4
5
6
7
select 
last_name,
department_id
from
employees
where
department_id<>90;

逻辑表达式查询

作用:用于连接条件表达式

逻辑运算符:&&, || , !, and , or , not
&&和and: 两个条件都为true, 结果为true,反之为false
||和o 只要有一个为true,结果就为true
!和no 如果链接的条件本身为false,结果为true,反之为false

案例一:查询工资在10000到20000之间的员工名、工资以及奖金

1
2
3
4
5
6
7
8
select
last_name,
salary,
commission_pct
from
employees
where
salary >=10000 and salary <=20000

案例二:查询部门编号不是在90到110之间,或者工资高于15000的员工信息

1
2
3
4
5
6
select 
*
from
employees
where
not(department_id>=90 and department_id<=110) or salary > 15000;

模糊查询

like , between , in ,is null

1. like:

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

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

1
2
3
4
5
6
select 
*
from
employees
where
last_name like '%a%';

案例二:查询员工名中第三个字符为e,第五个字符为a的员工名和工资

1
2
3
4
5
6
select 
last_name,salary
from
employees
where
last_name like '__n_l%';

案例三:查询员工名中第二个字符为_的员工名

用转义字符 \ 或者下面这种方法

1
2
3
4
5
6
select
last_name
from
employees
where
last_name like '_$_%' escape '$';

2. between and

特点:

  1. 包含临界值
  2. 两个临界值不要调换顺序
  3. 提高了语句简洁度

案例一:查询员工编号在110到120之间的员工信息

1
2
3
4
5
6
select
*
from
employees
where
employees_id between 100 and 120;

3. in

特点:

  1. 提高语句简洁度
  2. in列表的值类型必须一致或兼容
  3. 不支持通配符

案例一:查询员工的年龄属于23,24,25岁的员工信息

1
2
3
4
5
6
select
*
from
employees
where
emp_age in (23,24,25);

4. is null

特点:

  1. =或<>不能判断null值
  2. is null 或 is not null 可以判断null值

案例一:查询没有奖金的员工信息

1
2
3
4
5
6
selsct
*
from
employees
where
commission_pct is null;

本文标题:Mysql查询-条件查询

文章作者:尚先森

发布时间:2019年08月26日 - 16:45:20

最后更新:2022年07月27日 - 14:06:04

原始链接:https://imauu.gitee.io/2019/08/26/Mysql查询-条件查询/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

尚先森 wechat
有任何问题可以扫描上方二维码私聊我哦😊
您的支持是我创作的最大动力^_^