select 查询列表 from 表名 where 筛选条件;
条件查询的查询顺序为:
第一步:表名,表示查询来自哪张表
第二步:筛选条件,在选定表中按条件来筛选
第三步:查询,查询筛选后的字段web
>
,<
,=
,!=
,<>
,>=
,<=
&&
=and
,||
=or
,!
=not
&&
和and
:若是两个都为TRUE
,结果为TRUE
like
,between and
,in
,is null
>12000
的员工信息
select * from employees where salary>12000;
90
号的员工名
和部门编号
select last_name,department_id from employees where department_id!=90;
10000
到20000
之间的员工名、工资、奖金
select last_name,salary,commission_pct from employees where salary>=10000 and salary<=20000;
部门编号
不是在90-100
之间,或者工资
高于15000
的员工
select * from employees where department_id<90 or department_id>110 or salary>15000;
1.like
通常和通配符搭配使用
通配符
:
%
:任意多个字符,包含0个字符
_
:任意单个字符sql
员工名
包含字符a
的信息select * from employees where last_name like '%a%';
n
,第五个字符为l
的员工名和工资select last_name,salary from employees where last_name like '__n_l%';
员工名
中第二个字符为_
的员工名"转移"select last_name from employees where last_name like '_\_%'; # 此时通配符_做为普通字符须要转移,反斜杠\_ select last_name from employees where last_name like '_$_%' escape '$'; # escape是为了说明$符号为转移字符,在不用反斜杠的状况下
2.between and
优点:
①使用between and能够提升语句的简洁度
②包含临界值
③两个临界值不能调换顺序安全
员工编号
在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
含义:
判断某字段的值是否属于in列表中的某一项
特色:
①使用in
比or
提升语句简洁度
②in
列表中的值类型必须一致或兼容。等价于=
svg
IT_PROT
、AD_VP
、AD_PRES
中的一个员工名和工种编号select last_name,job_id from employees where job_id = 'IT_PROT' or job_id='AD_VP' or JOB_ID='AD_PRES'; select last_name,job_id from employees where job_id in ('IT_PROT','AD_VP','AD_PRES');
4.is null
特色:
=
或<>
不能判断null
值
is null
或is not null
能够判断null
值函数
select last_name,commission_pct from employees where commission_pct is null;
select last_name,commission_pct from employees where commission_pct is not null;
5.安全等于 <=>
spa
select last_name,commission_pct from employees where commission_pct <=> null;
12000
的员工信息select last_name,commission_pct from employees where salary <=> 12000;
is null
与<=>
的比较
is null
:
仅仅能够判断null
值,但可读性高,建议使用
<=>
:
既能够判断null值,又能够判断普通的数值,但可读性低code
select last_name,department_id,salary*12*(1+ifnull(commission_pct,0)) as 年薪 from employees;
select 查询列表 from 表 [where 筛选条件] order by 排序列表 [asc|desc]
xml
asc
表明升序,desc
表明降序,默认是升序asc
order by
能够放单个字段,多个字段,表达式,函数,别名
order by
字句通常放在查询语句最后面,limit
字句除外排序
select * from employees order by salary desc; select * from employees order by salary asc; # 默认从低到高
>=90
的员工信息,按入职时间前后排序
[添加筛选条件]select * from employees where department_id>=90 # 添加筛选条件 order by hiredate asc; # 再日期前后顺序排序
[按表达式排序]
select * ,salary*12*(1+ifnull(commission_pct,0)) 年薪 from employees order by salary*12*(1+ifnull(commission_pct,0)) desc; # 按表达式排序
[按别名排序]
select *,salary*12*(1+ifnull(commission_pct,0)) 年薪 from employees order by 年薪 desc; # 按别名排序
[按函数排序]
select length('last_name') 字节长度,last_name,salary from employees order by length('last_name') desc; # 按函数length排序
[按多个字段排序]
select * from employees order by salary asc,employee_id desc; # 多字段排序