sql
// 绿色的只为注释不必写入
| cmd | 解释 |
|---|---|
CREATE DATABASE ceshi1 CHARACTER SET ‘utf8’ COLLATE ‘utf8_general_ci’; |
创建数据库 |
| ==表的操作== | |
| drop table if exists 表名 | 删除表 (drop table if exists) |
| alter table 表名(too) add 要添加的字符段及类型(isdelete int unsigned); | 在已有表中添加字符类型 |
| insert into 表名 values(表中所有的类型,所对应类型的值。。。); | 增加数据 |
| insert into 表名(id,name)(指定添加类型,name) values(所对应类型…‘小明’) | 指定类型添加 |
| ==逻辑删除(对重要的数据通常使用逻辑删除)== | |
| upaert (表名)too set isdelete=1 where id=1; | (0代表存在,1代表删除); |
| upaert (FaceDeepFeat)too set isdelete=1 where UserID=1; | (0代表存在,1代表删除); |
| delete from (表名)too where (要删除的类型值, id=2); | 删除语句 |
| delete from (表名)too; | 删除所有数据 |
| update too(表名) set (要修改的类型和值,例如name=‘小红’) where (约束,id=3); | 修改语句 (update … set. .. where) |
| select * from table_name | 查询表的所有信息 |
| select *from 表名(too) where isdelete=0; | 使用了逻辑删除,表示查询没删除的数据 |
| ==在查询中可以给表,及表中的字符类型取别名,在同时查询多个表时用处极大== | |
| select A.name as 姓名, A.age as 年龄, A.height as 身高 from too as A | 取别名,在与from相邻的那个别名要加一个空格符!!! |
| ==去重复 (distinct)== | |
| select (去重复)distinct age from too; ,avg(age) as 平均from too grop by calss; | 查询各个班级的最大年龄,最小年龄,平均年龄 |
| ==比较查询 (where)== | |
| select name from too where id=1; | 查询id为1的数据 |
| ==逻辑查询(and,or, not)== | |
| select *from too where age=18 and home=’上海’; | 查询年龄等于18且家在上海的同学 |
| select *from too where age=’18’ or age=20; | 查询年龄是18或则20的同学 |
| select *from too where not age=30; | 查询年龄不是30的同学 |
| ==空判断(is) == | |
| select *from too where name is NULL; | 查询名字是否为空 |
| select *from too where name=’’; | 查询名字是否没写 |
| ==模糊查询 (like, %,_)== | |
| select *from too where name like ‘冯%’; | 查询姓冯的同学 |
| select *from too where name like ‘%冯%’; | 查询带冯的同学数据 |
| select *from too where name like ‘冯_’ | 查询名字姓冯且只有一个字的同学 |
| select *from too where name like ‘__’ | 查询名字是两个字的同学 |
| ==范围查询(in(), between and)== | |
| select *from too where age in(18,20); | 查询年龄是18或20的同学 |
| select *from too where name in(‘大乔’,’西施’); | 查询名字是大乔西施的同学 |
| select *from too where not age between 18 and 19; | 查询不是18到19岁的同学 |
| select *from too where age between 18 and 25; | 查询18到25岁的同学 |
- 同时增加多条语句
insert into 表名(指定要添加的类型,name,age) values(所对应的类型‘小明’,20),(所对应的类型‘小红’,18),(所对应的类型,‘晓晓’,19);
- 创建表(先写名称,再写类型,最后写约束) (create table )
1 | create table 表名 |
– 排序 (order by asc, order by desc(降序)) 默认为升序的
//当需要多个排序时,优先排查第一个相同进行下一个类型的排查
select *from too order by age; – 升序
select *from too order by age desc; – 降序
select *from too order by convert(name using gbk); –按中文排序要先转换,字符修改为gbk
//多次排序
//查询所有同学按班级和学号倒序排查,排序中班级优先
select *from too order by calss desc, id desc;
– 聚合函数 (count, sum, max, min, avg)
//count函数注意(count后面的括号内可以是类型也可以是号,如果是类型,类型中右NULL的话是不会被统计的!!!)
select count()from too; – 求数据总数
select sum(age)from too; – 求年龄和
select max(age)from too; – 求年龄最大值
select min(age)from too; – 求年龄最小值
select avg(age)from too; – 求年龄的平均值
select max(age),min(age),avg(age)from too; – 同时求其最大值最小值平均值
round函数保留小数点采用四舍五入
// 查询所有商品的平均值并保留其最后两位小数点
1 | select round(avg(price),2) from goods; |
– 分组 (group by having(having是分组后的筛选项不可以单独使用) )
//group by 后面可以跟多个定义,分组时也需要全部符合定义才可分为一组
select age,count()from too group by age; –查询各个年龄有多少人
select calss,max(age) as 最大,min(age) as 最小,avg(age) as 平均 from too group by calss; –查询各个班级的最大年龄,最小年龄和平均年龄;
select calss,sex,count()from group by calss,sex; –查询各个班级的男女总和
– 查询1班除外的最大年龄,最小年龄和平均年龄
1 | select calss,max(age),min(age),avg(age),sum(age),count(*) from too group by calss having calss!='1班'; |
对比where和having
where是对from后面指定的表进行数据筛选,属于对原始数据的筛选
having是对group by的结果进行筛选
– 分页 (limit 后面两个整数,第一个代表起始位,第二个代表后移多少位)
分页公式== 一业的总数*(i-1),业的总数!!!
例如::共30条数据,每页只要5条数据
for(inti=1; i<=5; i++)
1 | j = 5*(i-1); |
select *from too limit j,5;
获取部分行::
select *from too limit 0,3 //获取表中前三行的数据
select *from too limit 2,3 //获取第三行到第五行的数据
– 连接查询之等值连接
select *from too,scores,conuses where too.id between ‘001’ and ‘003’ and too.id=scores.stubentno and scores.courseNo = conuses.courseNo;//此语句会产生笛卡尔积和临时表,消耗内存过大,不实用;
– 连接查询之内连接 (select *from inner join on)
select *from too inner join socres inner join conuses on too.id=socres.stubentno and socres.courseNO=conuses.courseNO; //不会产生笛卡尔积和临时表,性能高;
– 左连接(left join)::join前面的表称为左表,把左表的所有值查询出来
1 | select *from too left join scores on too.id=scores.stubentno; |
– 右连接(right)::join后面的表称为右表,把右表的所有值查询出来
1 | select *from scores right join conuses on scores.courseno=conuses.courseno; |
– 自动关联(链表)使用场景::数据之间有上下级关系的 ,在一个表中存储所有数据
自动关联就是对一个表进行多次查询,在查询中表名一定一定一定要起别名,否则报错;
– 查询河南省有几个市
1 | select *from areas as a, areas as b where a.aid=b.pid and a.atitle='河南省'; |
– 子查询
– 标量子查询(查询=(查询))
select *from too where age=(select min(age) from too); – 子查询年龄最小同学的所有信息
– 列子查询(in(查询多个))(一列多行)
//查询所有18岁同学的成绩信息,要求显示成绩;
1 | select score from scores where stubrntno in(select id from too where age=18); |
– 行子查询(where (类型)=(子查询)) 子查询返回一行多列的
//查询女生中年龄最小的信息
1 | select *from too where (sex,age)=(select sex,age) from too where sex='女' order by age limit 1; |
–表子查询(返回多行多列)
– 查询虞姬和武则天的成绩
1 | select score from scores inner join(select *from too where name in('虞姬','武则天')) as t where scores.stubentno=t.id; |
子查询中的关键字:
in, any,some, all::
in 范围查询 或 (不可以使用赋值号)
any(任何一个) (可以使用赋值号)
some(一些) (可以使用赋值号)
all(所有) (可以使用赋值号)
– 数据分表
在表中查询出数据直接添加到表中
//把表中商品名称查出来加入到新表中((cate_name)是要加入到哪个类型下)
1 | insert into goods_cate (cate_name)select cate from goods group by cate; |
– 新建一个表并直接把查询出来的数据加入到表中,如果表中没有相同的字段名,系统会自动以查询出来的字段名新建一个,也可以使用as增加别名来同步字段名
drop table if exists goods_brand;
create table goods_brand
(
id int unsigned primary key auto_increment,
name varchar(10)
)
select distinct brand_name as name from goods;
– 把另一个表中查出的数据添加到所需的表中 (update set)
update goods
inner join goods_cate on
goods.cate=goods_cate.cate_name
1 | set goods.cate=goods_cate.id; |