语法:
insert into 表名 (字段名,...) values (值,...);
特点:
1、要求值的类型和字段的类型要一致或兼容
2、字段的个数和顺序不一定与原始表中的字段个数和顺序一致
但必须保证值和字段一一对应
3、假如表中有可以为null的字段,注意可以通过以下两种方式插入null值
①字段和值都省略
②字段写上,值使用null
4、字段和值的个数必须一致
5、字段名可以省略,默认所有列
语法:
insert into 表名 set 字段=值,字段=值,...;
1.方式一支持一次插入多行,语法如下:
insert into 表名(字段名,..) values(值,..),(值,...),...;
2.方式二支持子查询,语法如下:
insert into 表名
查询语句;
一、修改单表的记录
语法:
update 表名 set 字段=值,字段=值 where 筛选条件;`
二、修改多表的记录【补充】
语法:
update 表1 别名
left|right|inner join 表2 别名
on 连接条件
set 字段=值,字段=值
where 筛选条件;
一、删除单表的记录
语法:
delete from 表名 where 筛选条件 limit 条目数
二、级联删除[补充]
语法:
delete 别名1,别名2 from 表1 别名
inner|left|right join 表2 别名
on 连接条件
where 筛选条件
语法:
truncate table 表名
select distinct address from stu;
select name as 名字 ,age as 年龄,from user;
SELECT userCode AS 名字 FROM smbms_user;
select 列名1, ... ,列名n from 表名
where 条件 -- 1、条件
group by 列名 -- 2、分组
having 条件 -- 3、条件
order by 列名 -- 4、排序
limit 开始,条数 -- 5、分页
select * from 表名
#多列列名使用,隔开
select 列名 from 表名
select 列名 [as] 别名,列名n [as] 别名n
#列和固定数值
select 列名+-*/%数值 from 表名
#列和列
select 列名+-*/%列名 from 表名
select concat(列名1,列名2,...)合并后列名 from 表名
select * ,'常量' 列名 from 表名
==(=,>,<,>=,<=,!=<>,如果是null,需要写为is\is not )==
select * from 表名 where 列名=值
select * from 表名 where 列名1=值 and 列名2=值
#不同列
select * from 表名 where 列名1=值 or 列名2=值
#同一列的不同值
select * from 表名 where 列名 in (值1,值2,...)
select * from 表名 where 列名 not in (值1,值2,...)
#查询l开头的名字,任意长度
select * from 表名 where 列名 like 'l%'
#查询l开头的名字,固定长度
select * from 表名 where 列名 like 'l__'
#查询包含o的名字
select * from 表名 where 列名 like '%o%'
select * from 表名 where 列名 between 下界值 and 上界值
select * from 表名 order by 列名
select * from 表名 order by 列名 asc
select * from 表名 order by 列名 desc
select * from 表名 order by 列名1 desc,列名2 desc
#起始位置默认为0,查询不包括起始位置
select * from 表名 limit 起始位置,每页条目数
#假如要显示的页数为page,每一页条目数为size
select 查询列表
from 表
limit (page-1)*size,size;
关于某一列进行操作,和行没有关系
count() | 返回结果集中行的数目 |
---|---|
max() | 返回结果集中所有值的最大值 |
min() | 返回结果集中所有值的最小值 |
sum() | 返回结果集中所有值的总和 |
avg() | 返回结果集中所有值的平均值 |
聚合函数null值不参与运算,如果希望null值也参与那么需要ifnull()函数处理
select 函数名(列名) from 表名
select 分组列名,聚合函数 from 表名 group by 分组列名
#一般和聚合函数一起使用
#如,查询student表中的男女两个分组中的最大年龄和最小年龄
select ssex,max(sage),min(sage) from student group by ssex
group by后不可以使用where进行筛选,需要使用having关键字
如果能用where筛选数据的话,绝不使用having
#如,查询年龄大于11的各个年龄段的人数
select sage,count(sage) from student group by sage having sage > 11
#可以使用where就不要用having
select sage,count(sage) from student where sage > 11 group by sage
1. 将2个表的数据进行拼接,针对拼接后的重复数据 去重显示
select 列名 from 表名1 where 条件
union
select 列名 from 表名2 where 条件
2. 将2个表的数据进行拼接,针对拼接后的重复数据 不去重显示
select 列名 from 表名1 where 条件
union all
select 列名 from 表名2 where 条件
#sql92
select * from 表1,表2;
#sql99
select * from 表1 cross join 表2;
笛卡尔积组合,形成数据没有价值
select 列名 from A表
inner join B表 on 关联条件;
select 列名 from A表
left join B表 on 关联条件;
select 列名 from A表
right join B表 on 关联条件;
#查询1号课程成大于2号课程的学生id
select * from sc s1
inner join sc s2
on s1.sno=s2.sno
where s1.cno=1 and s2.cno=2 and s1.score>s2.score
select distinct 列名 from 表名;
嵌套查询,就是指一个sql语句里面还有一条sql语句
将查询的结果(可以作为值,也可以作为表),再次被sql语句使用
#查询李华老师所带课程
select * from course where tno =
(
select tno from teacher where tname = '李华'
)
#查询李华老师所带学生的信息
-- 1 获取李华老师的编号
select tno from teacher where tname = '李华'
-- 2 根据老师的编号获取所带学科的编号
select cno from course where cno in
(
select tno from teacher where tname = '李华'
)
-- 3 根据学科编号获取学生的编号
select sno from sc where cno in
(
select cno from course where cno in
(
select tno from teacher where tname = '李华'
)
)
-- 4 根据学生编号,获取学生的信息
select * from student where sno in
(
select sno from sc where cno in
(
select cno from course where cno in
(
select tno from teacher where tname = '李华'
)
)
)
#查询学生表中,年龄大于张三或tony的学生信息
select * from student where sage > any(
select sage from student where sname = '张三' or sname = 'tony'
)
#查询学生表中,年龄大于张三并且大于tony的学生信息
select * from student where sage > all(
select sage from student where sname = '张三' or sname = 'tony'
)
#查询student表中,按照年龄显示是否成年
select *,
case
when sage >= 18 then '成年'
else '未成年'
end
from student