【sql常用知识】基本增删查改 查询专题 多表查询(内查询,外查询) 子查询 分组查询 limit 分页 like模糊查询 having 和 where 的区别

基本增删查改

select * from 表名 where 条件;
insert into 表名 values(数据);
delete from 表名 where 条件;
update <表名> set <列名=更新值> [where <更新条件>]

查询专题

在查询中使用as进行重命名 或者 直接空格

select Personname as ps from a where xingbie=‘男’

select * from category c,products p where c.cid = p.category_id;

查询返回限制行数(关键字:top percent)

select top 6 name from a
显示列name的前6行,top为关键字
select top 60 percent name from a
显示列name的60%,percent为关键字

查询排序(关键字:order by , asc , desc)

select name from a where chengji>=60 order by desc
asc升序,desc降序

使用between在某个范围内进行查询

select * from a where nianling between 18 and 20

使用in在列举值内进行查询

select name from a where address in (‘北京’,‘上海’,‘唐山’)
查询表a中address值为北京或者上海或者唐山的记录

like模糊查询 limit分页

Select * from person where name like “%长沙%” limit 0,20

like模糊查询

模糊查询 dname like " % 张%"
查的张某某 或者 xx张xx

limit 分页

limit N,M
N:从哪一行开始(初始从0开始,不是1), M:每一页的行数
page 第几页
N=(page-1)*M
比如 第二页 N=(2-1)*M

分组查询

查询工资大于2000的,工资总和大于9000的部门名称以及工资和
select deparmant,GROUP_CONCAT(salary), SUM(salary) from employee
WHERE salary > 2000
GROUP BY deparmant
HAVING sum(salary) > 9000
ORDER BY SUM(salary) DESC;

使用group by进行分组查询,使用having子句进行分组筛选
having 和 where 一样,但 having 只能用于 group by

having 和 where 的区别

having 是在分组后对数据进行过滤,where 是在分组前对数据进行过滤
having后面可以使用分组函数(统计函数),where后面不可以使用分组函数
where 是对分组前记录的条件,如果某行记录没有满足where字句的条件,那么这行记录不会参加分组;而having是对分组后数据的约束

多表查询(可以理解成在笛卡尔集的基础上进行条件筛选)

内连接(本质就是在笛卡尔集上加条件)

隐式内连接:select * from A,B where 条件;
显式内连接:select * from A inner join B on 条件;(效率更高)
区别
》使用关键字 inner join
》前者在笛卡尔集的基础上筛选,后者在原始表上筛选

隐式内连接:select * from A,B where 条件;

select * from category c,products p where c.cid = p.category_id;

显式内连接:select * from A inner join B on 条件;(效率更高)

select * from category c inner	join products p on c.cid = p.category_id;

外连接 查询

两个表中选一个表将数据全部输出,另一个表没有对应数据则输出NULL

左外连接

select * from A left outer join B on 条件;
以左表为主,左表中的数据全部输出,右表中如果没有同等的数据则补NULL

# 左外连接
select * from province p left outer join city c on p.pid = c.pid_fk;

右外连接

select * from A right outer join B on 条件;
以右表为主,右表中的数据全部输出,左表中如果没有同等的数据则补NULL

# 右外连接
select * from province p right outer join city c on p.pid = c.pid_fk;

子查询

select的嵌套:一个select的查询结果作为另一个select查询语法的一部分

select * from product where cid = 1
select cid from category where cname='电子'

select *from product where cid = (select cid from category where cname='电子')

一对多

create database day13_2
use day13_2
》创建主表(分类表)
create table category(
	cid int primary key auto_increment,
	cname varchar(20)
)
》创建从表(商品表)
create table product(
	pid int primary key auto_increment,
	pname varchar(20),
	price double,
	cid int -- 外键 表示属于哪个分类
)
》给主表和从表之间添加外键约束 
`alter table 从表 add [constraint] [外键名称] foreign key (从表外键字段名) references 主表 (主表的主键);`
alter table product add  foreign key (cid) references category(cid)
》给主表添加数据(随便添加)
insert into category value(null,'电子')
insert into category value(null,'服装')
》给从表添加数据(添加数据是必须依赖主表)
insert into product value(null,'联想',2000,1)
insert into product value(null,'华为',4000,1)	
insert into product value(null,'真维斯',100,2)	

like模糊查询 limit分页

Select * from person where name like “%长沙%” limit 0,20

like模糊查询

模糊查询 dname like " % 张%"
查的张某某 或者 xx张xx

limit 分页

limit N,M
N:从哪一行开始(初始从0开始,不是1), M:每一页的行数
page 第几页
N=(page-1)*M
比如 第二页 N=(2-1)*M

猜你喜欢

转载自blog.csdn.net/mighty_Jon/article/details/109154791
今日推荐