基础查询
一、起别名作用
①便于理解
②如果要查询的字段有重名的情况,使用别名可以区分开来
方式一:使用as
SELECT 100%98 AS mod;
SELECT last_name AS 姓,first_name AS 名 FROM employees;
方式二:使用空格
SELECT last_name 姓,first_name 名 FROM employees;
案例:查询salary,显示结果为 out put
SELECT salary AS "out put" FROM employees;
mysql> select distinct employee_id as '部门id' from employees;
+----------+
| 部门id |
+----------+
| 178 |
| 200 |
| 201 |
| 202 |
| 206 |
+----------+
5 rows in set (0.00 sec)
二、去重 distinct
案例:查询员工表中涉及到的所有的部门编号
SELECT DISTINCT department_id FROM employees;
mysql> select distinct employee_id as '部门id' from employees;
+----------+
| 部门id |
+----------+
| 178 |
| 200 |
| 201 |
| 202 |
| 206 |
+----------+
5 rows in set (0.00 sec)
三、+号的作用
java中的+号:
①运算符,两个操作数都为数值型
②连接符,只要有一个操作数为字符串
mysql中的+号:
仅仅只有一个功能:运算符
select 100+90; 两个操作数都为数值型,则做加法运算
select '123'+90;只要其中一方为字符型,试图将字符型数值转换成数值型
如果转换成功,则继续做加法运算
select 'john'+90; 如果转换失败,则将字符型数值转换成0
select null+10; 只要其中一方为null,则结果肯定为null
操作如下:
mysql> select 'john'+90;
+-----------+
| 'john'+90 |
+-----------+
| 90 |
+-----------+
1 row in set, 1 warning (0.00 sec)
mysql> select 8+3;
+-----+
| 8+3 |
+-----+
| 11 |
+-----+
1 row in set (0.00 sec)
mysql> select null+30;
+---------+
| null+30 |
+---------+
| NULL |
+---------+
1 row in set (0.00 sec)
四、连接concat( )
案例:查询员工名和姓连接成一个字段,并显示为 姓名
操作如下:
mysql> select concat(last_name,first_name) as '姓名' from employees;
+------------------+
| 姓名 |
+------------------+
| K_ingSteven |
| KochharNeena |
| De HaanLex |
| PataballaValli |
| GietzWilliam |
+------------------+
5 rows in set (0.00 sec)
五、显示出表employees的全部列,各个列之间用逗号连接,列头显示成OUT_PUT
SELECT
CONCAT(`first_name`,',',`last_name`,',',`job_id`,',',IFNULL(commission_pct,0)) AS out_put
FROM
employees;
mysql> select concat(last_name,',',first_name,',',ifnull(commission_pct,0)) from employees;
+------------------------+
| output |
+------------------------+
| K_ing,Steven,0.00 |
| Kochhar,Neena,0.00 |
| De Haan,Lex,0.00 |
| Hunold,Alexander,0.00 |
| Gietz,William,0.00 |
+------------------------+
5 rows in set (0.00 sec)
六.in
含义:判断某字段的值是否属于in列表中的某一项
特点:
①使用in提高语句简洁度
②in列表的值类型必须一致或兼容
③in列表中不支持通配符
案例:查询员工的工种编号是 IT_PROG、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');
七、is null
=或<>不能用于判断null值
is null或is not null 可以判断null值
案例1:查询没有奖金的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct IS NULL;
案例1:查询有奖金的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct IS NOT NULL;
八、安全等于 <=>
案例1:查询没有奖金的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct <=>NULL;
案例2:查询工资为12000的员工信息
SELECT
last_name,
salary
FROM
employees
WHERE
salary <=> 12000;
IS NULL:仅仅可以判断NULL值,可读性较高,建议使用
<=> :既可以判断NULL值,又可以判断普通的数值,可读性较低
条件查询
语法:
select
查询列表
from
表名
where
筛选条件;
分类:
一、按条件表达式筛选
简单条件运算符:> < = != <> >= <=
二、按逻辑表达式筛选
逻辑运算符:
作用:用于连接条件表达式
&& || !
and or not
&&和and:两个条件都为true,结果为true,反之为false
||或or: 只要有一个条件为true,结果为true,反之为false
!或not: 如果连接的条件本身为false,结果为true,反之为false
三、模糊查询
like
between and
in
is null
特点:
一般和通配符搭配使用
通配符:% 任意多个字符,包含0个字、_ 任意单个字符
案例1:查询员工名中包含字符a的员工信息
select
*
from
employees
where
last_name like '%a%';#abc
案例2:查询员工名中第三个字符为e,第五个字符为a的员工名和工资
select
last_name,
salary
FROM
employees
WHERE
last_name LIKE '__n_l%';
案例3:查询员工名中第二个字符为_的员工名
SELECT
last_name
FROM
employees
WHERE
last_name LIKE '_$_%' ESCAPE '$';
#2.between and
①使用between and 可以提高语句的简洁度
②包含临界值
③两个临界值不要调换顺序
四、排序查询
语法:
select
要查询的东西
from 表
where
条件
order by 排序的字段|表达式|函数|别名 【asc|desc】
特点:
1、asc代表的是升序,可以省略,desc代表的是降序
2、order by子句可以支持 单个字段、别名、表达式、函数、多个字段
3、order by子句在查询语句的最后面,除了limit子句
1、按单个字段排序
SELECT * FROM employees ORDER BY salary DESC;
2、添加筛选条件再排序
案例:查询部门编号>=90的员工信息,并按员工编号降序
SELECT *
FROM employees
WHERE department_id>=90
ORDER BY employee_id DESC;
3、按表达式排序
案例:查询员工信息 按年薪降序
SELECT *,salary*12*(1+IFNULL(commission_pct,0))
FROM employees
ORDER BY salary*12*(1+IFNULL(commission_pct,0)) DESC;
4、按别名排序
案例:查询员工信息 按年薪升序
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪
FROM employees
ORDER BY 年薪 ASC;
5、按函数排序
案例:查询员工名,并且按名字的长度降序
SELECT LENGTH(last_name),last_name
FROM employees
ORDER BY LENGTH(last_name) DESC;
6、按多个字段排序
###案例:查询员工信息,要求先按工资降序,再按employee_id升序
SELECT *
FROM employees
ORDER BY salary DESC,employee_id ASC;
五、分组查询
语法:
select 查询列表
from 表
where 筛选条件】
group by 分组的字段
order by 排序的字段】;
特点:
1、和分组函数一同查询的字段必须是group by后出现的字段
2、筛选分为两类:分组前筛选和分组后筛选
分组前筛选 针对原始表、在group by前、用的关键字where
分组后筛选 针对group by后的结果集、在group by后、用的关键字having
3、分组可以按单个字段也可以按多个字段
4、可以搭配着排序使用
注1:分组函数做筛选不能放在where后面
注2:where——group by——having
注3:一般来讲,能用分组前筛选的,尽量使用分组前筛选,提高效率
1.查询公司员工工资的最大值,最小值,平均值,总和
SELECT MAX(salary) 最大值,MIN(salary) 最小值,AVG(salary) 平均值,SUM(salary) 和
FROM employees;
2.查询员工表中的最大入职时间和最小入职时间的相差天数 (DIFFRENCE)
SELECT MAX(hiredate) 最大,MIN(hiredate) 最小,(MAX(hiredate)-MIN(hiredate))/1000/3600/24 DIFFRENCE
FROM employees;
SELECT DATEDIFF(MAX(hiredate),MIN(hiredate)) DIFFRENCE
FROM employees;
SELECT DATEDIFF('1995-2-7','1995-2-6');
3.查询部门编号为90的员工个数
SELECT COUNT(*) FROM employees WHERE department_id = 90;
四、练习
(一)分组筛选
案例1:查询每个工种的员工平均工资
SELECT AVG(salary),job_id
FROM employees
GROUP BY job_id;
案例2:查询每个位置的部门个数
SELECT COUNT(*),location_id
FROM departments
GROUP BY location_id;
2、可以实现分组前的筛选
案例1:查询邮箱中包含a字符的 每个部门的最高工资
SELECT MAX(salary),department_id
FROM employees
WHERE email LIKE '%a%'
GROUP BY department_id;
案例2:查询有奖金的每个领导手下员工的平均工资
SELECT AVG(salary),manager_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY manager_id;
3、分组后筛选
案例:查询哪个部门的员工个数>5
#①查询每个部门的员工个数
SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id;
#② 筛选刚才①结果
SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*)>5;
案例2:每个工种有奖金的员工的最高工资>12000的工种编号和最高工资
SELECT job_id,MAX(salary)
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING MAX(salary)>12000;
案例3:领导编号>102的每个领导手下的最低工资大于5000的领导编号和最低工资
SELECT manager_id,MIN(salary)
FROM employees
where manager_id>102
GROUP BY manager_id
HAVING MIN(salary)>5000;
4.添加排序
案例:每个工种有奖金的员工的最高工资>6000的工种编号和最高工资,按最高工资升序
SELECT job_id,MAX(salary) m
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING m>6000
ORDER BY m ;
5.按多个字段分组
案例:查询每个工种每个部门的最低工资,并按最低工资降序
SELECT MIN(salary),job_id,department_id
FROM employees
GROUP BY department_id,job_id
ORDER BY MIN(salary) DESC;
(二)按条件表达式筛选
案例1:查询工资>12000的员工信息
SELECT
*
FROM
employees
WHERE
salary>12000;
案例2:查询部门编号不等于90号的员工名和部门编号
SELECT
last_name,
department_id
FROM
employees
WHERE
department_id<>90;
(三)按逻辑表达式筛选
案例1:查询工资z在10000到20000之间的员工名、工资以及奖金
mysql> select last_name,salary,commission_pct from employees where salary>=10000 and salary<=20000;
+-----------+----------+----------------+
| last_name | salary | commission_pct |
+-----------+----------+----------------+
| Kochhar | 17000.00 | NULL |
| De Haan | 17000.00 | NULL |
| Greenberg | 12000.00 | NULL |
| Raphaely | 11000.00 | NULL |
| Russell | 14000.00 | 0.40 |
| Partners | 13500.00 | 0.30 |
| Errazuriz | 12000.00 | 0.30 |
| Cambrault | 11000.00 | 0.30 |
| Zlotkey | 10500.00 | 0.20 |
| Tucker | 10000.00 | 0.30 |
| K_ing | 10000.00 | 0.35 |
| Vishney | 10500.00 | 0.25 |
| Ozer | 11500.00 | 0.25 |
| Bloom | 10000.00 | 0.20 |
| Abel | 11000.00 | 0.30 |
| Hartstein | 13000.00 | NULL |
| Baer | 10000.00 | NULL |
| Higgins | 12000.00 | NULL |
+-----------+----------+----------------+
18 rows in set (0.00 sec)
案例2:查询部门编号不是在90到110之间,或者工资高于15000的员工信息
SELECT
*
FROM
employees
WHERE
NOT(department_id>=90 AND department_id<=110) OR salary>15000;
案例1:查询员工编号在100到120之间的员工信息
SELECT
*
FROM
employees
WHERE
employee_id >= 120 AND employee_id<=100;
#----------------------
SELECT
*
FROM
employees
WHERE
employee_id BETWEEN 100 AND 120;
(四)分组查询
1.查询各job_id的员工工资的最大值,最小值,平均值,总和,并按job_id升序
SELECT MAX(salary),MIN(salary),AVG(salary),SUM(salary),job_id
FROM employees
GROUP BY job_id
ORDER BY job_id;
2.查询员工最高工资和最低工资的差距(DIFFERENCE)
SELECT MAX(salary)-MIN(salary) DIFFRENCE
FROM employees;
3.查询各个管理者手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内
SELECT MIN(salary),manager_id
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING MIN(salary)>=6000;
4.查询所有部门的编号,员工数量和工资平均值,并按平均工资降序
SELECT department_id,COUNT(*),AVG(salary) a
FROM employees
GROUP BY department_id
ORDER BY a DESC;
5.选择具有各个job_id的员工人数
SELECT COUNT(*) 个数,job_id
FROM employees
GROUP BY job_id;
(五)排序查询
1.查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序
SELECT last_name,department_id,salary*12*(1+IFNULL(commission_pct,0)) 年薪
FROM employees
ORDER BY 年薪 DESC,last_name ASC;
2.选择工资不在8000到17000的员工的姓名和工资,按工资降序
SELECT last_name,salary
FROM employees
WHERE salary NOT BETWEEN 8000 AND 17000
ORDER BY salary DESC;
3.查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序
SELECT *,LENGTH(email)
FROM employees
WHERE email LIKE '%e%'
ORDER BY LENGTH(email) DESC,department_id ASC;