一、基本sql语句
1.查看指定数据库中的所有表
show tables from mysql;
2.查看自己在哪个数据库中
select database();
3.查看表的结构
desc user;
4.查看当前数据库版本
select version();
5.终端里查看
mysql --version 或 mysql -V
6.注释
(1)单行注释:#注释语句
(2)单行注释:-- 注释语句
(3)多行注释:/*注释语句*/
二、基础查询
# 进阶1:基础查询
/*
1.语法
select 查询列表 from 表明;
2.特点
(1)查询列表可以是:表中的字段、常量值、表达式、函数
(2)查询的结果是一个虚拟的表格
*/
USE myemployees ;
# 1.单字段查询
SELECT `last_name` FROM employees;
# 2.多字段查询
SELECT `last_name`, `first_name`, `email` FROM employees;
# 3.所有字段查询
SELECT * FROM employees;
# 4.起别名,两种方式:AS或空格;如果别名有特殊符号用引号
SELECT 100%98 AS 结果;
SELECT last_name AS '姓', first_name AS '名' FROM employees;
SELECT last_name 姓, first_name 名 FROM employees;
# 5.去重
# 案例:查询员工表中涉及到所有部门编号
SELECT DISTINCT `department_id` FROM employees;
# + 作用:只作为数值相加
# 案例:员工姓、名连接成一个字段
SELECT CONCAT(last_name, first_name) AS '姓名' FROM employees;
三、条件查询
# 进阶2:条件查询
/*
1.语法
select 查询列表 from 表名 where 筛选条件;
2.分类
(1)按条件表达式筛选[> = < != <> >= <=]
(2)按逻辑表达式筛选[&& || ! and or not]
(3)按模糊筛选[like、between and、in、is null]
*/
# 一、条件查询
# 案例1:查询工资大于12000员工信息
SELECT * FROM employees WHERE salary > 12000;
# 案例2:查询部门编号!=90的员工名和部门编号
SELECT first_name, department_id FROM employees WHERE department_id<>90;
# 二、逻辑表达式
# 案例1:查询工资在10000到20000之间的员工名、工资以及奖金
SELECT first_name, salary, commission_pct FROM employees WHERE salary >=10000 AND salary <=20000;
# 案例2:查询部门编号不是90到110之间,或者工资高于15000的员工信息
SELECT * FROM employees WHERE NOT(department_id >= 90 AND department_id <= 110) OR salary >15000;
# 三、模糊查询
/*
通配符:
① % 任意多个字符,包含0个字符
② _ 任意单个字符
*/
# 案例1:查询员工名中包含字母a的员工信息(like关键字)
SELECT * FROM employees WHERE last_name LIKE '%a%'
# 案例2:查询员工名中第三个字母n,第五个字符为l的员工名、工资
SELECT last_name, salary FROM employees WHERE last_name LIKE '__n_l%';
# 案例3:查询员工名中第二个字符为 _ 的员工名,需要使用\对_进行转义
SELECT last_name, salary FROM employees WHERE last_name LIKE '_\_%';
# 案例4:查询员工编号在100到120之间的员工信息(between and 是包含临界值的)
SELECT * FROM employees WHERE employee_id BETWEEN 100 AND 120;
# 案例5:查询员工的工种ID是IT_PROG/AD_VP/PU_MAN中之一的员工名工种ID(in 关键字)
SELECT last_name, job_id FROM employees WHERE job_id IN ('IT_PROG', 'AD_VP', 'PU_MAN');
# 案例6:查询没有奖金的员工名和奖金率(is null、is not 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;
# 案例7:安全等于
SELECT last_name, commission_pct FROM employees WHERE commission_pct <=> NULL;
SELECT last_name, commission_pct FROM employees WHERE commission_pct <=> 0.20;
/*
IS NULL:仅仅可以判断NULL值(√)
<=>:既可判断NULL值,也可判断数值
*/
# 查询员工号为176的员工姓名和部门号、年薪(如果利率为NULL则为0计算)
SELECT
last_name, department_id, salary*12*(1+IFNULL(commission_pct, 0)) AS annual_salary
FROM
employees
WHERE
employee_id=176;
四、排序查询
# 进阶3:排序查询
/*
语法:
select 查询列表 ③
from 表 ①
where 筛选条件 ②
order by 排序列表[asc | desc] ④
特点:
① 如果不写,默认升序
② order by 支持单个字段、多个字段、表达式、函数、别名
③ order by 一般放查询语句最后面,limit子句除外
*/
# 案例1:查询员工信息,要求工资从高到低排序(asc升序,desc降序)
SELECT * FROM employees ORDER BY salary DESC;
SELECT * FROM employees ORDER BY salary ASC;
# 案例2:查询部门编号>=90的员工信息,按入职时间的先后进行排序
SELECT * FROM employees WHERE department_id >= 90 ORDER BY hiredate ASC;
# 案例3;按年薪高低显示员工信息和薪资
SELECT *, salary*12*(1+IFNULL(commission_pct, 0)) AS annual_salary
FROM employees
ORDER BY annual_salary DESC;
# 案例4:按员工名长度显示员工姓名和工资
SELECT last_name, salary, LENGTH(last_name) AS name_length
FROM employees
ORDER BY name_length DESC;
# 案例5:查询员工信息,要求先按工资排序,再按员工编号排序
# 先按照工资升序,如果工资相同再按照员工id降序
SELECT * FROM employees ORDER BY salary ASC, employee_id DESC;
五、常见函数
# 进阶4:常见函数
/*
调用:select 函数名(实参列表) from 表;
分类
(1)单行函数-做处理的
concat、length、ifnull等
(2)分组函数-做统计的,又称聚合函数
*/
USE myemployees;
# 一、字符函数
# (1)length - 获取参数值的字节个数
SELECT LENGTH('helloworld');
# 查看数据库所支持支持字符集
SHOW VARIABLES LIKE '%char%';
# (2)concat - 拼接字符串
SELECT CONCAT(first_name, '_', last_name) AS '姓名' FROM employees;
# (3)upper/lower - 大写、小写
SELECT UPPER('join');
# 案例:将姓变大写、名变小写,然后拼接
SELECT CONCAT(UPPER(first_name),'_',LOWER(last_name)) AS '姓名' FROM employees;
# (4)substr/substring - 截取字符,索引从1开始
SELECT SUBSTR('李莫愁爱上陆展元', 6) AS out_put;
SELECT SUBSTR('李莫愁爱上陆展元',1,3) AS out_put; ->李莫愁
# 案例:姓名中首字母大写,其他小写然后用_拼接,显示出来
SELECT CONCAT(UPPER(SUBSTR(first_name,1,1)),'_',LOWER(SUBSTR(first_name,2))) AS out_put FROM employees;
# (5)instr - 显示子串在字符串中第一次出现的索引,如果不存在返回0
SELECT INSTR('helloworld', 'wo') AS out_put;
# (6)trim - 去除左右相同字符
SELECT TRIM(' hello ') AS out_put;
SELECT TRIM('a' FROM 'aaaaaaaahelloworldaaaaaa') AS out_put; ->helloworld
# (7)lpad - 左填充/rpad - 右填充
SELECT LPAD('中华',10,'*') AS out_put; ->********中华
# (8)replace
SELECT REPLACE('张无忌爱周芷若','周芷若','赵敏') AS out_put;
# 二、数学函数
# (1)round - 四舍五入
SELECT ROUND(1.35); ->1
SELECT ROUND(1.351, 2); ->1.35,小数点后保留2位
# (2)ceil - 向上取整,返回>=该参数的最小整数
SELECT CEIL(1.02); ->2
# (3)floor - 向下取整
SELECT FLOOR(-9.99); -》-10
# (4)truncate - 截断
SELECT TRUNCATE(1.699999, 1); ->1.6
# (5)mod - 取余
SELECT MOD(10, 3); ->1
# 三、日期函数
# (1)now - 返回当前系统日期,时间
SELECT NOW();
# (2)curdate - 返回当前系统日期,不包含时间
SELECT CURDATE();
# (3)curtime - 返回当前系统时间,不包含日期
SELECT CURTIME();
# (4)可获取指定的部分,年月日时分秒
SELECT YEAR(NOW()); - 2020
SELECT YEAR('1998-01-01'); - 1998
# 查询员工入职年份
SELECT YEAR(hiredate) FROM employees;
SELECT MONTH(NOW());
SELECT MONTHNAME(NOW()); - 支持英文
# (5) str_to_date - 将日期格式的字符串转换成指定格式的日期
SELECT STR_TO_DATE('9-13-1999','%m-%d-%Y'); - 1999-09-13
# 案例:查询入职日期为1992-04-03
SELECT * FROM employees WHERE hiredate = STR_TO_DATE('4-3 1992', '%c-%d %Y');
# (6) date_format - 将日期转换为字符
SELECT DATE_FORMAT('2018/6/6', '%Y年%m月%d日');
# 四、其他函数
SELECT VERSION(); - 数据库版本
SELECT DATABASE(); - 当前数据库
SELECT USER(); - 当前用户
# 五、流程控制函数
# (1) if
SELECT IF(1>5, '大','小'); - 小
SELECT last_name, commission_pct, IF(commission_pct IS NULL, '没奖金', '有奖金') AS '备注' FROM employees;
# (2) case
/*
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1;
when 常量2 then 要显示的值2或语句2;
...
else 要显示的值n或语句n;
end
*/
# 案例:查询员工工资,要求
# 部门号=30,显示的工资为1.1倍
# 部门号=40,显示的工资为1.2倍
# 部门号=50,显示的工资为1.3倍
# 其他部门,显示原工资
SELECT salary AS '原始工资', department_id,
CASE department_id
WHEN 30 THEN salary * 1.1
WHEN 40 THEN salary * 1.2
WHEN 50 THEN salary * 1.3
ELSE salary
END AS '新工资'
FROM employees;
序号 |
格式符 |
功能 |
1 |
%Y |
四位的年份 |
2 |
%y |
两位年份 |
3 |
%m |
月份(01/02...) |
4 |
%c |
月份(1/2/...) |
5 |
%d |
日(01/02...) |
6 |
%H |
小时(24小时制) |
7 |
%h |
小时(12小时制) |
8 |
%i |
分钟(00/01...) |
9 |
%s |
秒(00/01...) |
六、分组函数
# 分组函数
/*
功能:用统计使用;和分组函数一同查询的字段要求是group by后的字段
分类:全部忽略null值
(1)sum - 求和
(2)avg - 平均值
(3)max - 最大值
(4)min - 最小值
(5)count - 计算个数
*/
SELECT SUM(salary) FROM employees; - 691400
SELECT AVG(salary) FROM employees;
SELECT MAX(salary) FROM employees;
SELECT MIN(salary) FROM employees;
SELECT COUNT(salary) FROM employees;
# 分组函数和distinct(去重)结合使用
SELECT SUM(DISTINCT salary) FROM employees; - 397900
SELECT COUNT(DISTINCT salary), COUNT(salary) FROM employees;
# count函数详细介绍,count(*)统计行数
SELECT COUNT(*) FROM employees;
SELECT COUNT(1) FROM employees;
七、分组查询
# 进阶5:分组查询
/*
select 分组函数, 列(要求出现在group by后面)
from 表名
where 筛选条件
group by 分组列表
order by 子句
*/
# 案例1:查询每个工种的最高工资
SELECT MAX(salary) AS '最高工资', job_id
FROM employees
GROUP BY job_id;
# 案例2:查询每个位置上的部门个数
SELECT COUNT(*), location_id
FROM departments
GROUP BY location_id;
# 案例3:查询邮箱中包含a字符的,每个部门的平均工资
SELECT AVG(salary) AS '平均工资', department_id
FROM employees
WHERE email LIKE '%a%'
GROUP BY department_id;
# 案例4:查询奖金的每个领导手下员工最高工资
SELECT MAX(salary), manager_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY manager_id;
# 案例5:查询哪个部门的员工大于2
# ①查询每个部门员工个数
# ②根据①的结果进行筛选,查询哪个部门的员工个数>2
SELECT COUNT(*) AS num, department_id
FROM employees
GROUP BY department_id
HAVING num > 2;
# 案例6:查询每个工种有奖金的员工的最高工资>12000的工种编号和最高工资
# ①每个工种有奖金的员工的最高工资
# ②根据①筛选,最高工资大于12000的
SELECT MAX(salary), job_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING MAX(salary) > 12000;
# 案例7:查询领导编号>102的每个领导手下的最低工资>5000的领导编号是哪个,以及其最低工资
# ①查询每个领导手下的员工固定最低工资
# ②添加筛选条件,编号大于102
SELECT MIN(salary), manager_id
FROM employees
WHERE manager_id > 102
GROUP BY manager_id
HAVING MIN(salary) > 5000;
#案例8:按员工姓名长度分组,查询每一组员工个数,筛选员工个数>5的有哪些
SELECT COUNT(*),LENGTH(last_name)
FROM employees
GROUP BY LENGTH(last_name)
HAVING COUNT(*) >5;
# 案例9:按多个字段分组,查询每个部门每个工种的员工的平均工资
SELECT AVG(salary), department_id, job_id
FROM employees
GROUP BY department_id,job_id;
# 案例10:查询每个部门每个工种的员工的平均工资,工资升序
SELECT AVG(salary) AS avg_salary, department_id, job_id
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id,job_id
ORDER BY avg_salary ASC;