SQL入门(二)

一、分组计算

1.创建分组(group by)

(1)使用 group by 关键词:select col1,count( * ) from table group by col1;
select countrycode from world.City group by countrycode ,返回 countrycode 的唯一值,相当于使用 distinct 关键词。在这里插入图片描述
select countrycode,avg(Population) as avg_pop from world.City group by countrycode 相当于按照 countrycode 中的每一组计算 population 的平均数。
在这里插入图片描述
(2)Group by 子句可以包含任意数量的列;即可以按照多个列分组。
在这里插入图片描述

注意:
(1)select 检索的列必须是 group by 的列,即检索的列会被 group by 的列限制。
(2)null 值将作为单独一组

2.过滤分组(having)

(1)使用 having 关键字,规定包含与排除哪些分组:select col1,count() from table group by col1
having count(
) > 10;
(2)与 where 的区别在于:where 作用于行,having 作用于组;where 在数据分组前过滤,having 在数
据分组后过滤(意味着 where 先执行,having 后执行)。where 过滤排除的行不在分组中。
在这里插入图片描述

3.分组与排序

使用 order by 可以对组进行排序:select col1,count() as num from table group by col1 having
count(
) > 10 order by num
在这里插入图片描述

二、子查询

子查询(subquery):嵌套在其他查询中的查询

1.利用子查询进行过滤

作为 where 关键词的字句,进行数据过滤:select col1 , col2 from table where col3 in (select col
from table2)
注意:
(1)列必须匹配:确保子查询中 select 语句具有与 where 字句中相同数量的列
(2)子查询经常与 IN 操作符结合使用,但是也可以与等于/不等于结合使用。
在这里插入图片描述

2.作为计算字段使用子查询

在 select 关键字之后使用,作为结果字段:select col1 , col2 , (select col1 from table2 where
table1.col = table2.col) as col3 from table1
注意:
子查询中的 where 语句必须限定列名,否则就是与自身作比较,这样是没有意义的:where
table1.col = table2.col
在这里插入图片描述

三、表联结(join)

1. inner join(取两表交集)
select a.col1,b.col2 from table1 a inner join table2 b on a.col = b.col and a.col1 = b.col2
在这里插入图片描述
在这里插入图片描述

**2.left join **
取左边的表全集,右边表没有的部分按照空值进行联结,语法与 inner join 一样
select a.col1,b.col2 from table1 a left join table2 b on a.col = b.col and a.col1 = b.col2

3.right join
取右边的表全集,左边表没有的部分按照空值进行联结,语法与 inner join 一样
select a.col1,b.col2 from table1 a right join table2 b on a.col = b.col and a.col1 = b.col2
4.多个表联结
按照从左到右的顺序依次联结,圆括号可改变顺序。
在这里插入图片描述

四、组合查询(union)

union将各条查询结果组合起来,组合成一列。列的名字为第一条select语句中的列名。
1.使用union
在各条select语句之间加上union关键字:select col1 from table1 union select col2 from table 2
union select col3 from table3
2.union规则
(1)union必须由两条或者两条以上select语句组成
(2)每个子句都必须包含相同数的列、聚合函数(函数需相同,否则无实际意义,如 union前面的表对数据进行求和,union 后面的表对同一列的数据进行平均,两个数据放在一起无意义)等,相同的列不必具有相同的名字,union 会以前面的列的名字作为组合列的名字
(3)数据类型必须兼容,union 的列必须是相同的数据类型。
3.包含或取消重复的行
union自动对结果进行去重,union all不会去重
4.对组合查询结果排序
只能在最后一条select语句之后使用order by:
select col1 from table1 union select col2 from table2 union select col3 from table3 order by
col1
在这里插入图片描述
在这里插入图片描述

五、表的创建(create table)

补充:MySQL中的数据类型
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.创建表
(1)create table创建表:需要指定表的名字,列的名字,数据类型,是否允许缺失值,主键名字等
表列的名字与定义,使用逗号隔开
creat table table1 ( id int not null auto_increment , col1 int not null , col2 char(20) null ,
primary key(id) );
在这里插入图片描述
(2)创建表时,如果该表已经存在,则会报错,若不想其报错,可在 create table 后加上 if not exists 语句,这样,如果该表已存在,会提示警告信息,不会报错。
create table if not exists soton_exercises.jerry_firsttable(id int not null auto_increment,name
char(20),age int,height float,primary key(id));如果该表不存在则创建,若存在则警告,不报错。

2.null值与默认值
(1)null值就是没有或者缺值,null值不是空串。
(2)允许null值的列也允许在插入行时不给出该列的值
(3)不允许null值的列不接受该列没有值的行
(4)创建表时可以指定默认值,插入时如果该列没有指定值时,就使用默认值
create table table1 ( id int not null auto_increment , col1 int not null default 1 , col2 char(20)
null , primary key ( id ) );

3.主键
(1)主键的值必须唯一,如果使用多列作为主键,那么组合值必须唯一
(2)主键不允许有null值
creat table table1 ( id int not null auto_increment , sub_id int not null ,col1 int not null , col2 char(20) null , primary key ( id ,sub_id ) );

4.auto_increment
(1)每个表只允许一个auto_increment列,而且必须被索引,如设置为主键
(2)在使用insert增加一行时,该列自动增量

六、插入数据(insert into)

插入数据行名和value的值需要顺序对应,插入顺序没关系,auto_increment不需要插入。
1.插入完整的行
insert into table1(col1,col2,col3) values(value1,value2,value3)

2.插入多个行
insert into table1(col1,col2,col3) values(value1,value2,value3)(value1_,value2_,value3_)

3.插入检索出的数据
将一条select语句的结果插入到表中:
insert into table1(col1,col2,col3) select col1,col2,col3 from table2

七、表的操作

1.更新表(alter table)
alter table table1 add col int;给 table1 表增加一列
alter table table1 drop column col; 删除 table1 中的一列
注意:
(1)尽可能避免使用alter table,在表设计之初就应该尽可能考虑周全
(1)在更改之前,应该对表数据做一次备份,以防出错

2.删除表(drop table)
drop table table1;

3.重新命名表(rename table)
rename table table1 to table2;

八、更新和删除数据

1.更新数据(update…set…)
update table1 set col1 = value1,col2 = value2 where condition;
不要省略where字段,否则会更新表中的全部值
三步:更新的表——>列名和他们的值——>更新过滤的条件

2.删除数据(delete from)
delete from table1 where condition;
不要省略where字段,否则会删除表中的全部值

3.一些原则
(1)MySQL数据库是没有撤销按钮的,所以一定要仔细注意自己的操作。
(2)一定要使用where子句,除非确认更改每一行。
(3)在使用update和delete之前,应该先使用select进行测试。

九、SQL执行顺序

  1. from 笛卡尔乘积
  2. on 过滤筛选器
  3. join 保留表
  4. where 过滤器
  5. group by 分组
  6. agg_function 聚合函数计算
  7. having 组过滤
  8. select 检索数据
  9. distinct 去重
  10. order by 排序
  11. limit 限制
发布了20 篇原创文章 · 获赞 10 · 访问量 1455

猜你喜欢

转载自blog.csdn.net/Jerry_Chang31/article/details/103707660