1.按条件表达式筛选
条件运算符:><=!=<>>=<=2.按逻辑运算符筛选
逻辑运算符:and or not
3.模糊查询
特点:一般和通配符一起使用
like(模糊查询)
between and(包含临界值,但是临界值不能随便交换)in()
is null
is not nullisnull(参数) 判断字段是否为null,是返回1,不是返回0<=>:安全等于,是判断等于,等于就返回true(可以判断null值和普通值)
4.通配符:
%:任意多个字符,包含0个字符
-:任意单个字符
escape:转义字符
\:转义字符
案例1:查询员工工资大于12000
select *from 员工表 where 工资>12000;
案例2:查询部门编号不等于90的员工名称和部门编号
select *from 员工表 where 部门编号!=90;
案例3:查询工资在10000到20000之间的
select *from 员工表 where 工资>10000 and 工资 <=20000;
案例4:查询员工表中name包含“张”的员工
select *from 员工表 where name like '张%';
案例5:查询员工表中包含“_张”的员工
select *from 员工表 where name like "$_张" escape "$";//自己指定转义,推荐
select *from 员工表 where name like "\_张";
案例6:查询员工表年龄在20到40之间的员工
select *from 员工表 where age between 20 and 40;
案例7:查询员工的工种名称等于study,class,other的员工
select *from 员工表 where typeName in(study,class,other);
案例8:查询没有奖金的员工和奖金率
select name,commission_pct from 员工表 where commission_pct is null (这里不能用比较运算符,比较运算符是无法判断null的)
案例9:查询没有奖金的,且工资小于18000的salary,last_name
select salary,last_name from 员工表 where 奖金 is null and salary <18000;
基础查询
select 查询列表 from 表名
1.查询列表可以是字段,常量,表达式
2.查询结果是一个虚拟的表
eg:select 字段名 from 表名
select *from 表名
select 100;//查一个常量,字符和日期型必须引号起来,数值不用3.查询函数,函数都必须有返回值
排序查询
语法:select 查询列表 from 表名 where 条件 order by [desc(降序)|asc(升序,默认升序)]length(filed):函数主要用于查询字节长度
对多个字段进行排序
select *from 表名字
order by filde1 desc,filde2 desc,filde3 asc;
排序查询案例
eg:查询员工姓名和部门号和年薪,按年薪降序排序,按姓名升序排序
select 姓名,部门号,12*每月工资 from 员工表 order by 年薪 DESC, 姓名 ASC;
eg:查询工资不在8000到17000的员工姓名和工资,按工资降序排序
select 姓名,12*每月工资 as'年薪'from 员工表 where 12*每月工资 not between 8000 and 17000 order by 年薪 DESC;