5.1 查询单个字段
查询员工姓名(注:在SQL语句中不区分大小写;SQL语句以“;”分号结束)。
SQL查询语句:select ename from employee;
select语句后面跟的是字段名称,select是关键字,select和字段名称之间采用空格隔开,from表示将要查询的表,它和字段之间采用空格隔开。
5.2 查询多个字段
查询员工姓名和员工工资(注:多个字段查询时,字段与字段之间用“,”隔开)
SQL查询语句:select ename,sal from employee;
查询多个字段,select中的字段采用逗号间隔即可,最后一个字段,也就是在from前面的字段不能使用逗号了。
5.3 查询全部字段
我们可以将所有字段放到 select 语句之后,这种方案不方便,但是比较清楚,我们可以采用下面的方式查询全部字段。
SQL查询语句:select * from employee;
注:采用select * from employee;,虽然简单,但是*号不是很明确,并且select *语句会先去编译,将“*”转换成字段,建议查询全部字段将相关字段写到select语句的后面,在以后 java 连接数据库的时候,是需要在Java程序中编写SQL语句的,这个时候编写SQL语句不建议使用select * 这种形式,建议写明字段,这样SQL语句的可读性强。
5.4 查询员工年薪
查询出员工编号、员工姓名、员工年薪
SQL查询语句:select empno,ename,sal*12 from employee;
注:字段上可以使用数学表达式,只要SQL语句中有 select 关键字,不会修改底层数据库字段的值。
5.5 对查询出的字段重命名
SQL查询语句:
①select empno,ename,sal*12 asyearsal from employee;或
select empno,ename,sal*12 yearsal from employee;
②select empno,ename,sal*12 as ‘年薪’from employee;或
select empno,ename,sal*12 '年薪' from employee;或
select empno,ename,sal*12 as “年薪” from employee;或
select empno,ename,sal*12 “年薪” from employee;
注:
1、重命名为中文时必须加单引号或双引号;
2、标准SQL语句中类型为字符串时必须加单引号,加单引号适用于任何数据库;
3、SQL语句中类型为字符串时也可加双引号,只适用于MySQL数据库中;
4、为了SQL语句的通用性,建议全部使用单引号。
5.6 条件查询
条件查询需要用到 where 语句,where 必须放到 from 语句表的后面。执行顺序:先from再where过滤后再检索出来。
MySQL支持如下运算符:
运算符 | 说明 |
= | 等于 |
<>或!= | 不等于 |
< | 小于 |
<= | 小于等于 |
> | 大于 |
>= | 大于等于 |
between...and... | 两个值之间,等同于 >= and <= |
is null | 为null(is not null 不为空) |
and | 并且 |
or | 或者 |
in | 包含,相当于多个or(not in不在这个范围中) |
not | not可以取非,主要用在is 或in中 |
like | like称为模糊查询,支持%或下划线匹配 %匹配任意个字符 下划线,一个下划线只匹配一个字符 |
5.6.1 等号(=)运算符
举例1:查询工资为6000的员工信息
SQL语句:select empno,ename,sal from employeewhere sal = 4500;
举例2:查询姓名为“李四”员工信息
SQL语句:select * from employeewhere ename = "MARY";或
select * from employeewhere ename = 'MARY';
5.6.2 不等号(<>、i=)运算符
举例:查询工资不为5000的员工信息
SQL语句:select * from employeewhere sal <> 4500;或
select * from employeewhere sal != 4500;
5.6.3 between … and …运算符
举例:查询薪水为3500到5500的员工信息
SQL语句:select * from employeewhere sal between 4500 and 5500;或 select * from employeewhere sal >= 4500 and sal<= 5500;
注:关于between … and … 它是包含最大值和最小值的。
5.6.4 is null运算符
Null 为空,它不是一个数值,不是一个空串,为null可以设置这个字段不填值,如果查询为null的字段,采用is null。
举例1:查询部门编号为空的员工信息。
SQL语句:select * from employeewhere deptno = null;
以上无法查询出符合条件的数据,因为null类型比较特殊,必须使用 is null
SQL语句:select * from employee where deptno is null;
举例2:查询部门编号不为空的员工信息。
SQL语句:select * from employee where deptno is not null;
5.6.5 and运算符
and 表示并且的含义,表示所有查询条件必须同时满足。
举例:查询所有项目组长并且工资大于5000的员工信息
SQL语句:select * from employeewhere job='项目组长' and sal>5000;
5.6.6 or运算符
or表示所有查询条件至少有一个满足。
举例:查询所有项目经理和产品经理的员工信息
SQL语句:select * from employeewhere job='项目组长' or job='开发人员';
5.6.7 and与or的优先级
and的优先级高于or
举例:查询薪水大于5000,并且部门编号为20或30的员工
SQL语句:select * from employeewhere sal>5000and deptno=20 or deptno=30;(错误写法)
以上输出的结果不是我们预期的结果,薪水小于5000的也查出来了,原因是表达式的优先级导致的,首先SQL语句过滤了 sal > 5000 and deptno = 20,然后再将deptno = 30的员工合并过来,所以是不正确的。
正确写法:select * from employeewhere sal > 5000 and (deptno = 20 or deptno = 30);
注:关于运算符的问题不用死记硬背,没有把握的尽量采用括号。
5.6.8 in运算符
in表示包含的意思,完全可以采用or来表示,采用in会更简洁一些。
举例1:查询所有项目经理和产品经理的员工信息
SQL语句:select * from employeewhere job in('项目组长','开发人员');
举例2:查询出工资为4500和5500的员工信息
SQL语句:select * from employeewhere sal in(4500,5500);
举例3:查询出工资不是4500和5500的员工信息
SQL语句:select * from employeewhere sal not in(4500,5500);
5.6.9 not运算符
举例1:查询出工资不是4500和5500的员工信息
SQL语句:select * from employeewhere sal not in(4500,5500);或 select * from employee where sal!=4500and sal!=5500;
或 select * from employee where sal<>4500and sal<>5500;
举例2:查询部门编号不为空的员工信息。
SQL语句:select * from employee where deptno is not null;
5.6.10 like运算符
like可以实现模糊查询,like支持%和下划线匹配。
举例1:查询以姓名以 M 开头的所有员工
SQL语句:select * from employeewhere ename like 'M%';
举例2:查询姓名以N结尾的所有员工
SQL语句:select * from employeewhere ename like '%N';
举例3:查询姓名中包含O的所有员工
SQL语句:select * from employeewhere ename like '%O%';
举例4:查询姓名中第二个字符为A的所有员工
SQL语句:select * from employeewhere ename like '_A%';
举例5:查询姓名中倒数第二个字符为E的所有员工
SQL语句:select * from employeewhere ename like '%E_';
举例6:查询姓名中第三个字符为A的所有员工姓名
SQL语句:select * from employeewhere ename like '__A%';
5.7 查询结果排序
5.7.1 单一字段排序
排序采用 order by 子句,order by 后面跟上排序字段,排序字段可以放多个,多个采用逗号间隔,order by默认采用升序(asc),如果存在 where 子句,那么 order by 必须放到 where 语句后面。
举例1:按员工工资由小到大排序(系统默认由小到大)
SQL语句:select ename,job,sal from employee order by sal;
举例2:查询开发人员,并按工资由小到大排序(系统默认由小到大)
SQL语句:select ename,job,sal from employee where job='开发人员' order by sal;
注:如果包含 where 语句 order by 必须放到 where 后面,如果没有 where 语句 order by 放到表的后面。
5.7.2 指定排序规则
举例1:按员工工资由小到大排序(默认升序,升序关键字为asc)
SQL语句:select ename,job,sal from employee order by sal asc;
举例2:按员工工资由大到小排序(降序关键字desc)
SQL语句:select ename,job,sal from employee order by sal desc;
5.7.3 多个字段排序
举例:按工作和工资倒序排序
SQL语句:select ename,job,sal from employee order by job desc,sal desc;
注:如果采用多个字段排序,如果根据第一个字段排序重复了,会根据第二个字段排序。