单表、多表、子查询

七、单表查询

select . . . from . . . where . . . group by . . . having . . . order by . . . limit . . .

1、while 条件的使用:

​ 对表中数据的筛选和过滤

​ (1)判断的符号:

​ = > >= < <= != <>不等于

​ (2)拼接条件关键字:

​ and or not

​ (3)查询区间范围值:between

​ between 小值 and 大值 [小值,大值] 查询两者之间这个范围内所有数据

​ (4)查询具体某个值的范围:in

​ in(1,2,3) 指定范围

​ (5)模糊查询 like “%” “_” 通配符:

​ like “%a”:匹配以a结尾的任意长度的字符串

​ like “a%”:匹配以a开头的任意长度的字符串

​ like “%a%”:匹配含有字母a的任意长度的字符串

​ like “_a”:个数一共两个,以a结尾的字符串

​ like “a_”:个数一共两个,以a开头的字符串

(1)单条件的查询:
(2)多条件的查询
(3)关键字between... and...
(4)null 关键字:进行判定的时候使用is进行判定,不要用=判定
(5)关键字 in 在 ... 之重 查询
(6)模糊查询 like "%" "_" 通配符
(7)concat(拼接;可使用as 起别名)
(8)concat_ws(拼接的符号,参数1,参数2,...)
(9)内部可使用四则运算符(+ - * /)
2、group by 子句 分组分类

group by 字段 对数据进行分类,by后面接什么字段,select就搜索什么字段

group_concat 按照分类形式进行字段的拼接

聚合函数
	count 统计总数 * 所有
	max 统计最大值
	min 统计最小值
	avg 统计平均值
	sun 统计总合

一般境况下:分组 + 聚合函数 配合使用
3、having 过滤

having 数据在分类分组之后,进行二次数据过滤,一般是配合group by 使用,分组之后再过滤

4、order by 排序

按照字段进行排序

order by asc 默认升序:从小到大

order by desc 降序:从大到小

5、limit 限制查询条数(数据分页)

limit m, n :m代表从第几条数据开始查,n代表查询几条,m=0数据库表中的第一条数据

两个参数,参数1默认从0开始算,0是第一条,参数2是查询条数
select * from 表 limit 0,5
只查一条数据,默认从第一条
select * from 表 limit 1
找数据库当中最后一条数据
select * from 表 order by 字段 desc limit 1 
找数据库当中最后三条数据
select * from 表 order by 字段 desc limit 3 desc
6、可以使用正则表达式查询数据(不推荐使用,效率不高)
select * from 表 where 字段 regexp ".*on$"  # 以什么结尾,没有?,问号不能使用
select * from 表 where 字段 regexp "^程" 以什么开头
select * from 表 where 字段 regexp "^程.*金"

八、多表查询

1、内连接(内联查询):两个表或者多表满足条件的所有数据查询出来(两表之间共有的数据)

(1)两表查询:
select 字段 from 表1 inner join 表2 on 必要的关联条件
(2)多表查询:
select 字段 from 表1 inner join 表2 on 必要条件1 inner join 表3 on 必要条件3 ...
(3)as 起别名
(4)where 默认实现的就是内联查询

2、外连接

(1)左连接(左联查询 left join)以左表为主,右表为辅,完整查询左表所有数据,右表没有的数据补null
	select * from employee left join  department on employee.dep_id = department.id;
(2)右链接(右联查询 right join)以右表为主,左表为辅,完整查询右表所有数据,左表没有的数据补null
	select * from employee right join  department on employee.dep_id = department.id;
(3)全连接(全连接 union)所有数据都合并起来,空缺的补null
	select * from employee left join  department on employee.dep_id = department.id
	union
	select * from employee right join  department on employee.dep_id = department.id;

九、子查询

​ 子查询可以单独作为一个临时数据,临时的表,临时的字段,一般用在from where select 字句后面,可以通过查询出来的临时数据和另外的表联合,变成一张更大的表,再做单表查询查到想要的数据

子查询:嵌套查询

​ (1)sql语句当中又嵌套了另外一条sql语句,用()包起来,表达一个整体

​ (2)一般应用在from子句后面表达一张表,where子句后面表达一个条件

​ (3)查询速度从快到慢:单表查询 -> 联表查询 -> 子查询

(1)普通where写法
    select 
    d.id,d.name
    from
    employee as e,department as d
    where
    e.dep_id = d.id
    group by
    d.id,d.name
    having 
    avg(e.age) > 25;

(2)inner join
    select 
    d.id,d.name
    from
    employee as e inner join department as d on e.dep_id = d.id
    group by 
    d.id,d.name
    having 
    avg(e.age) > 25;
(3)子查询
	1.先选出平均年龄大于25岁的部门id
		select dep_id from employee group by dep_id having avg(age) > 25;
	2.通过部门id,找部门的名字
		select name from department where id in (201,202);
	3.综合拼接
		select id,name from department where id in (select dep_id from employee group by dep_id having avg(age) > 25);
(4)
	1.找技术部门对应id
		select id from department where name = "技术";
	2.通过id找员工姓名
		select name  from employee where dep_id = 200;
	3.综合拼接
		select name,dep_id  from employee where dep_id = (select id from department where name = "技术");
	
(5)联表写法(把null的数据露出来 , 员工的部门dep_id 为null,代表这个部门没人.)
	select 
		d.id,d.name
	from 
		department as d left join employee as e on d.id = e.dep_id
	where 
		e.dep_id is null
(6)子查询
	1.先查询,员工在哪些部门 (所有员工的分类分别是 200 201 202 204)
		select dep_id from employee group by dep_id
	2.把不在部门的数据找出来
		select id  from department where id not in (200,201,202,204);
	3.综合拼接
		select id,name  from department where id not in (select dep_id from employee group by dep_id)
	

十、带EXISTS关键字查询

exists 关键字,表达存在

如果内层sql能够查询到数据,返回True,外层sql执行查询语句

如果内层sql不能查到数据,返回False,外层sql不执行查询语句

select * from employee where exists (select * from employee where id = 1) # 能
select * from employee where exists (select * from employee where id = 100) # 不能

补充:约束的添加何删除

1、添加/删除 约束 not null
	alter table 表名 modify 字段名 类型
	alert table 表名 modify 字段 类型 not null
	alert table 表名 modify 字段 类型 
2、添加/删除 unique 唯一索引
	slter table 表名 add unique(字段)
	alter table 表名 drop index 字段
3、添加/删除 primary key
	alter table 表名 add primary key(字段)
	alter table 表名 drop primary key
4、添加/删除 foreign key 外键(先通过desc表找到外键名字,然后再删)
	alter table 表 drop foreign key 字段
	alert table 外键表 add foreign key(外键字段) reference 关联表(关联字段)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值