查询的基本结构
数据库的查询用select语句来实现查询。
select语句完整语法:
select 目标表的列名或列表达式集合
from 基本表或(和)试图集合
(where 条件表达式)
(group by 列名集合[asc][desc])
(having 组条件表达式(指定分组的条件))
(order by 列名(集合))
insert插入语句
insert into 表名(字段名1,字段名2…)
values(属性值1,属性值2)
update更新数据语句
update 数据表名 set 字段名=新的字段值 where 条件表达式
投影操作
查询语句里必须有的子句,关键字select
结构:select 列,列,…列n from 表名
表前缀:select 表名.列名 from 表名
给列设置别名:select 表名.列名 as 别名 from 表名
给表设置别名:select 表名.列名 from 表名 as别名
执行顺序是先执行from后的别名,前面要用别名去点列名
计算列
数据库管理系统中,不存在该列,通过计算后得到该列,但是数据库的数据还是不会变
语法:select 列名计算 from 表名
函数:concat()字符串函数
条件查询
单条件查询结构:
select 列1,列2,…from 表名 where 列=值;
多条件查询:用连接词and或or
SELECT e_name,e_money,e_deptid
FROM t_employee
WHERE e_money>5000 and e_money<8000;
范围查询: between给下限和and给上限
SELECT *
FROM t_employee
WHERE e_money BETWEEN 5000 and 8000;
模糊查询:
通配符: “—”匹配任何单个字符; “%”匹配零或多个任意字符
SELECT *
FROM t_employee
where e_name like “李%”;
查询为空的数据
– 不能使用=或!=
– 使用is null 或 is not null
SELECT *
FROM t_employee
where e_name is null;
去重关键字distinct
– 去重后 重复数据仅仅显示一条
select distinct f_city from t_employee;
返回限定行 limit
主要用于分页查询,只给一个数默认0开始
– select 列名1,列名2…from 表名 limit 开始序列号,返回行数
– 开始序列号从0开始
– 返回行数,如果表格剩余的行数不足,就只会返回剩余的行数
– limit仅限MySQL
– 6-9数据 limit 5.4
– ps:简写 limit 0-n–>limit n
select * from t_employee limit 0,8;
集合查询
– 查询技术部/销售部的员工
–查看不是集合里的数据加一个not
– 利用集合完成
select * from t_employee
where f_deptName in(‘技术部’,‘销售部’);
ORDER BY 列名1,列名2,列名3…
– 多列排序会先排第一列名(f_money),依次往后第一列数据相同时按照第二列顺序名(f_inDate)
在第一列名下有相同数据在依据第二列排序
– 对查询数据按照指定列进行排序
– 默认为升序(asc),降序(desc)加在列名后
SELECT*
from t_employee
order by f_money desc,f_inDate;
时间函数
– now() 年月日 时分秒
– select CURDATE() 年月日
– select year (“2020-12-21”) 得到指定时间的年份
– select date_add(“1997-12-23”,interval 500 day) interval后跟要计算的时间 后面跟计算单位
– select TIMESTAMPDIFF(year,“1998-12-21”,now()); (单位,小时间,大时间)计算小时间-大时间的时间间隔
select NOW()
select CURDATE()
select year (“2020-12-21”)获取指定日期的年份
select date_add(“1997-12-23”,
interval 500 year)
select TIMESTAMPDIFF(month,now(),“2024-02-20”);
– 查询员工的同时查询年龄
SELECT *,TIMESTAMPDIFF(year,f_birthday,NOW()) as age
from t_employee
– 查询员工的同时查询年龄以及工龄
SELECT *,TIMESTAMPDIFF(year,f_birthday,NOW()) as age,TIMESTAMPDIFF(year,f_inDate,NOW()) as workage
from t_employee
if(条件,结果为真的返回值,结果为假的返回值)
case when语句用于多分支,类比于if-ELSEIF
– case
– when 条件一 then 数据一
– when 条件二 then 数据二
– when 条件三 then 数据三
– …
– else 数据n
– end
SELECT*,
case
when f_money<=5000 then “低工资”
when f_money>=5000 and f_money<=8000 then “中工资”
when f_money>=8000 and f_money<=10000 then “较高工资”
else “高工资”
end as 工资等级
from t_employee
case when属于条件,from应该放在所有条件后面
update t_employee
set f_money= f_money+
case
when f_deptName=“技术部” then 500
when f_deptName=“销售部” then 800
when f_deptName=“广告部” then 200
END
聚合函数:
-
count函数,统计记录某列数据的行数
– select count(计算规范)from 表名
– 计算规范:统计所有行数,包括null; all 列名(默认 等价于 列名)统计所有非null行数;
distinct 列名 统计所有非null且不重复的行数
select count() from t_employee
SELECT all count(f_name) from t_employee
SELECT count(distinct f_deptName) from t_employee -
sum()函数
– all 列名 累加非null的值
– distinct 列名 去重后累加非null的值
select sum( f_money) from t_employee
- avg()函数
– all 列名 计算非null的值
– distinct 列名 去重后计算非null的值
select avg( f_money) from t_employee
– 计算平均值时加上null
select sum(f_money)/count(*)from t_employee
select max(f_money),min(f_money)from t_employee
分组查询
– group BY根据by后的规则对数据进行分组,分组后就相当于将表格划分为多个小表格,
聚合函数应用在多个小表格上,多列分组时只有多列数据都相同时才会分到一组
分组语句对投影列的限制,要求select中的列必须满足以下两个条件,否则为无效投影
- 分组列(group by后的列名)
- 聚合函数列
– select 列a ,列b,聚合函数() from 表名(count 会成为新列的,名称)
– where 过滤条件 group by 列a…
select f_deptName ,f_gender ,count(*) from t_employee
where f_money>7000 group by f_deptName,f_gender
聚合函数是在分组后执行,
分组又是在过滤条件(where)后执行
所有通过where来对聚合函数进行过滤
所以提供having语句对聚合函数数据进行过滤
select f_deptName , avg(f_money) from t_employee
group by f_deptName
having avg(f_money)>8000
where与having的区别
- where运行在分组前,因此不能执行任何聚合函数
- having运行在分组后,只能用于聚合函数的过滤
基本查询SQL的执行顺序
- 执行from
- where条件过滤
- group by分组
- 执行select投影列
- having条件过滤
- 执行order by排序
- 执行limit语句,返回限定行
后执行的子句,可以使用前执行子句中的计算结果,定义的别名