1.条件查询的语法
1 select
2 查询列表
3 from
4 表名
5 where
6 筛选条件
7 # 执行顺序为:from --> where --> select
返回顶部
2.条件查询分类:
1 # 1.按条件表达式筛选:
2 条件运算符:>,<,=,>=,<=,<>
3 # 2.按逻辑表达式筛选:
4 逻辑运算符:and,or,not
5 # 3.模糊查询:
6 like:包含关系,一般配合通配符使用.
7 between and:
8 1).使用between and可以提高语句的简洁度
9 2).包含临界值
10 3).两个临界值 between A and B 中, A <= B, A和B不能调换顺序.
11 in:in后面跟的列表中的值类型必须一致
12 isnull:在mysql中=与<>不能用于判断null值,使用isnull和isnotnull来判断null值
返回顶部
3.条件查询简单示例
1 # 1.条件表达式筛选: 筛选出员工工资高于10000的员工的姓名与工资
2 select concat(first_name, last_name), salary from employees where salary>10000;
3 # 2.逻辑表达式筛选: 筛选出员工工资高于10000和工资低于5000的员工的电话号码和姓名
4 select phone_number,salary from employees where salary<5000or salary>10000;
5 # 3.模糊查询:
6 (1).like的使用:查询员工名字中包含字符a的员工,使用通配符与like查询
7 select last_name from employees where last_name like '%a%';
8 注:通配符:%代表任意多个字符
9 _ 代表任意一个字符,如果查询条件中_作为正常字符使用,而不是通配符是可以使用转义方式实现,示例如下:查
询姓名中包含_的员工,两种方式:
10 1).select last_name from employees where last_name like '%\_%';
11 2).select last_name from employees where last_name like '%$_%' escape '$';
12 (此种方法称为:自定义转义字符,官方推荐. escape指定了$为转义字符)
13 (2).between and的使用:查询员工工资在10000到15000之间的所有员工的名字与工资
14 select last_name, salary from employees where salary between 10000and15000;
15 (3).in的使用:查询员工工资为:5000,10000,15000,20000的所有员工的名字与工资
16 select last_name, salary from employees where salary in(5000,10000,15000,20000);
17 (4).isnull的使用:
18 1).查询员工奖金率为空的所有员工的名字与奖金率
19 2).查询员工奖金率不为空的所有员工名字与奖金率
20 1).select last_name, commission_pct from employees where commission_pct isnull;
21 2).select last_name, commission_pct from employees where commission_pct isnotnull
返回顶部