数据分析入门(2)-- sql语句

1.常用数据类型

整数:int,bit,tinyint(有符号范围-128-127,无符号范围:0-255)

小数:decimal(表示浮点数),如decimal(5,2)表示共存5位数,小数占2位

字符串:varchar(不定长)、char

日期时间:date(xx年--xx月--xx日)、time、datetime(xx年--xx月--xx日 时:分:秒)

枚举类型:enum

注意:字符串text表示存储大文本,当字符大于4000时推荐使用

2.数据库常见操作

1.连接数据库

1.cmd中连接数据库

mysql -u root (账号) -p 密码

QQ截图20200229091746.jpg

2.直接启动mysq客户端

开始->输出mysql->启动my sql客户端

2.退出数据库

exit

3.显示有哪些数据库

show databases;

4.查看当前正在使用的数据库

select database();

5.使用某个数据库

use 数据库名;

6.显示数据库版本

select version()

7.显示当前时间

select now()

8.创建数据库

create database + 数据库名称;

create database + 数据库名称 charset=utf8; --指定字符集为utf8格式

9.查看数据库的创建语句

show create database + 数据库名称;

10.删除数据库

drop database demo;

11.查询所有函数

? function;

12.备份数据库

mysqldump -u root -p 密码 + 数据库名称 > 名字.sql

注:首先要退出mysql的连接

13恢复数据库

mysql -uroot -p 密码 + 数据库名称 < sql文件

3.数据表操作

1.查看当前数据库中的所有数据表

show tables;

2.创建表

语法:create table + 数据表名字(字段名 字段类型 字段约束)

eg: create table student(

id int unsigned primary key auto_increment, --添加字段id,无符号整型,主键,自动递增

name varchar(6) not null, --名字不为空

age tinyint unsigned default 0, --默认0

high decimal(5,2), --小数占两位,整数3位

gender enum ('男','女','保密') default '保密' --注意 :最后一个不用加 , 号

);

3.查看创建的表

show create table + 表名称;

4.查看表结构

desc + 表名;

5.修改表结构 alter + add/modify(修改字段类型)/change (修改字段类型和名字)/drog(删除)

1.添加不存在字段

语法:alter table + 表名 add 列名 类型/约束

eg: alter table student add birthday datetime default "2020-2-16 11:11:11" --添加生日字段,datetime类型

2.修改存在字段的类型

语法:alter table + 表名 modify 列名 类型/约束

eg:alter table student modify birthday date default "2020-3-2"; --将上面birthday字段类型修改为date

3.修改字段名字以及类型

语法:alter table + 表名 change 列名 类型/约束

eg:alter table student change birthday bir date default "2020-3-2" ; -- 将birthday字段修改为bir

4.修改表--删除字段

语法:alter table +表名 drop +字段名;

eg: alter table student drop bir; --删除bir

6.删除表

语法:drop table + 表名

eg: drop table student ; --删除student表

7.mysql中的注释

使用 -- 来进行注释

4.数据增、删、改、查操作

一.增加 insert

1.全列插入:insert into 表名 values (值1,值2, ......)

特点:值和表的字段的顺序一一对应,插入是是插入一条含所有字段的值的数据

QQ截图20200229091809.jpg

eg: insert into student values (0,'刘备',22) --上表中含有三个字段,故使用全列插入时,要与上表一 一 对应

Note:

1.这里id字段由于使用了自动递增,所以可以使用占位符

2.占位符:只有主键才能使用,如上例中,id设为了主键,占位符有三种形式 0,default,Null

2.指定列插入:insert into 表名(列1,......) values (值1,......)

特点:值和列一 一对应,在指定的某一列中进行插入

eg: insert into student (name,age) values('张飞',23);

3.多行插入(批量插入): insert into 表名(列1,...)values (值1,...),(值1,...),...

特点:一次插入多条数据

eg:

1.insert into student values(default,'朱八戒',22,'男'),(default,'鲁班',23,'男');

2.insert into student (name,age,sex) values('周瑜',23,'男'),('程咬金',25,'男');

二、删除 update

1.语法:update 表名 set 列1 = 值1,列2 = 值2, ...[where 条件]

Note:一定得加where条件,否则就变成了全表更新

eg: update student set sex = '男' where name = '关羽';

三、删除 delete(物理删除)

1.语法:delete from 表名 【where 条件】

Note:一定得加条件,否则你就等着跑路吧(手动滑稽)

eg: delete from student where id = 1;

四、查询 select

1.查询所有列

语法:select * from + 表名

eg:select * from student;

2.指定字段查询

语法:select 列1,列2,..... from 表

eg: select name,age from student;

1.使用as来对查询字段起别名

eg: select name as 姓名,age as 年龄 from student; --name起别名为 姓名

2.sql的完全形式 (做多表查询时要将表名带上

eg: select demo.student.name, demo.student.age from student; --demo数据库student表,可以省略

3.使用as来对表起别名

eg: select student.name, student.age from student; 可以写为 select s.name,s.age from student as s;

4.使用 distinct 消除重复行

eg : select distinct sex from student;

Note:当distinct后有多个字段时,只有当查询的多列的查询结果完全相同时才能去重

QQ截图20200229091818.jpg

eg: select distinct sex,age from student;

QQ截图20200229091826.jpg

3.指定条件查询 where

1.比较运算符 >、<、>=、<=、 !=

eg:

1.查询大于18岁的学生

select * from student where age > 18;

2.“不等于”的两种表示方法

select * from student where age != 18; or select * from student where age <>18;

2.逻辑运算符 and(与)、not(非)、or(或)

eg:

1.select * from student where age > 18 and sex = '女'; -- 查询18岁以上的女生

2.select * from student where age not 18 -- 年龄不是18岁的学生

3.select * from student where age > 18 or height > 180; -- 查询年龄 > 18 并且 身高 > 180的学生

3.模糊查找 like

1.% 表示任意字符可有可无

eg:

1.查询名字中 以 “小” 开始的名字

select * from student where name like '小%';

2.查询名字中包含 华 的名字

select * from student where name like "%华%"

2._ 表示任意一个字符

eg:

1.查询有两个字的名字

select * from student where name like "__";

2.查询有三个字的名字

select * from student where name like "___";

Note:sql还可以使用正则表达式来进行查询

eg: select * from student where name rlike "^刘";

4.范围查找 in、between、

1.in 表示在一个非连续的范围中(列举式

eg :

(1).select * from student where age in (18,20,30); -- 查询年龄为18、20、30的学生

(2). select * from student where age not in (18,20,30); -- 年龄不是18,34岁学生的信息

Note: not in 表示不在非连续的范围内

2.between ... and ... 表示在一个连续的范围内,两端都包含

eg:

(1).select * from student where age between 18 and 25; -- 查询年龄在18-25之间的学生

(2).select * from student where age not between 18 and 25; --查询年龄不在18-25岁之间的学生

Note: not between ... and ... 表示不在一个连续的范围内

5.空判断 null 不能够使用比较运算符

eg : select * from student where height is null;

6.排序 asc 、desc

语法: order by + 字段 + desc/asc(默认就是升序排序,所以 asc可以省略)

1.asc 从小到大排序,即升序

eg: select * from student order by age ; --按年龄从小到大排序

2.desc 从大到小排序,即降序

eg: 1.select * from student order by age desc; --按年龄降序排列

2.select * from student order by age asc, height desc; --按照年龄进行升序排序,如果年龄相同,则按照身高进行降序排序

Note:一般先进行筛选,在进行排序

7.聚合函数 count() 、sum()、max()、min()、avg()(作统计用)

语法:

1.count(*) 以行为单位来统计个数

eg: select count(*) from student; --统计班级总共的人数

Note: 也可以使用 select count (id) from student; --只不过效率比第一种低,count () 用来统计非空字符,如果统计列字段有为空的,则不会统计非空那行

2.max() 用来查找最大值

eg:select max(age) from student; -- 查询最大的年龄

3.select 语句的嵌套 --> 子查询

eg: select name from student where height = (select max(height) from student); -- 查询身高最高的学生姓名,Note: 加上 () ,表示优先执行

4.sum() 求总和

eg: select sum(age) from student; -- 查询所有学生年龄总和

5.avg() 求平均值

eg: select avg(age) from student; -- 查询所有学生年龄的平均值

6.round() 四舍五入

eg: round(select avg(age) from student,) --查询学生的平均年龄,并保留两位小数

Note:round()函数中含有两位操作数,第一个参数传入要进行取舍的函数,第二个用来传入要取舍的位数

8.分组 group by(分组的目的就是为了做聚合统计)

eg: select sex from student group by sex;-- 按照性别进行分组

1.先分组,后聚合

eg:1.select sex,count(*) from student group by sex; --计算每种性别的人数(聚合函数只会作用在分组之后的数据上

 

2.concat () 可以将字符串进行拼接

select sex,group_concat(name) from student group by sex; -- 查询每个性别分组数据中人的姓名

QQ截图20200229091835.jpg

.3.使用having 条件筛选,表示对于已经分组的数据做进一步的筛选

Note:对分组后的数据进一步的筛选只能用 having ,不能用 where

eg: 1.计算男性的人数

1).不使用聚合 select count(*) from student where sex = "男";

2).使用聚合 select sex,count(*) from student groupby sex having sex = "男";

注:having 和 where 的区别:1.having 表示对已经分组的数据做进一步的筛选,有having就一定有group by,有group by不一定有having ; where 是对源数据做筛选操作

2.计算每种性别的平均年龄

select sex,avg(age) from student group by sex;

3.查询平均年龄超过25岁的性别,以及姓名

select sex,group_concat(name) from student group by sex having avg(age) > 25;

4.查询每种性别中年龄最大的前几位

select sex,substring_index(group_concat(age order by age desc),',',2) from student group by sex;

QQ截图20200229091842.jpg

Note:出题频率很高

8.分页 limit,start,count,

语法:select 字段 + 表名 +limit start(数值),count(数据); 1)start 表示从哪里开始查询,默认值为0,可以省略,可以理解为跳过多少条数据

2)count 表示查询多少条

eg : select * from student limit 0,4;

Note: sql 语句结构 与优先级

QQ截图20200229091850.jpg

重点:sql语句执行的顺序:

1.from 表名

2.where ....

3.group by ......

4.select distinct

5.having .....

6.order by ......

7.limit,start,count

9.连接查询

1.连接查询:将两个表按照某种条件合并在一起

1)-笛卡尔积查询,有可能产生庞大的中间表

查询学生的姓名和对应的班级姓名(学生姓名在学生表中,班级姓名在班级表中)

select * from student,class where student.cls_id = class.id;

2)内连接查询(语法): table1 inner join table2 on 条件, 设置内连接条件,内连接:将满足连接条件的两张表合并成一张表

eg: 上例 select student.name, class.class_name from student inner join class on student.cls_id = class.id;

取别名: select s.name, c.class_name from student as s inner join class as c on s.cls_id = c.id;

3) 外连接查询 left join + right join 外连接:分主表和次表,由于现在是左外连接,左边表示的是主表,主表的数据全部显示(满足条件和不满足条件的都显示,不满足连接条件的以NULL填充)

  1. left join 左外连接查询

    eg: select s.name, c.class_name from student as s leftjoin class as c on s.cls_id = c.id;

  2. right join 右外连接查询

10.自关联

例子声明: 将省、市、区、县放于一张表中,设置pid、id、atitle字段,pid用来存储上级字段

QQ截图20200229091901.jpg

eg: 查询出广东省有多少个市

Note:想向成多张表,例如本例中将areas想象成两张表,一张省表,一张市表

select p.atitle,c.atitle from areas as p inner join areas as c on p.aid = c.pid where p.atitle = "广东省";

11.子查询

1.概念:在一个select语句中,嵌入了另外一个select语句,那么嵌入的select 就被称为子查询语句

2.主查询和子查询的关系

1)子查询是嵌入到主查询中

2)子查询是辅助主查询的,要么充当条件,要么充当数据

3) 子查询是可以独立存在的语句,是一条完整的select语句

eg: 查询身高最高的学生名字

select name from student where height = (select max(height) from student);

3.子查询语句根据查询的结果可以分为4种类型的子查询

1)标量子查询:子查询语句查询的结果是一行一列(就是一个最基本的语句)

eg: select name from student where height = (select max(height) from student);

2) 列级子查询: 子查询语句查询的结果是一列多行

eg: select * from student where age in (18,20,30)---(改成子查询) select * from student where age in (select age from student where age in 18,20,30); 主查询查的是学生的所有信息,子查询查询的是满足年龄在18,20,30

3)行级子查询: 实现行级子查询需要构建行元素

行元素:将多个字段合并成一个行元素,在行级子查询中会使用到行元素

eg: 查询班级年龄最大身高最高的学生

select * from student where (age,height) = (select max(age),max(height) from student);

note:这里将age和height合并成一个行元素

4)表级子查询:查询的结果是多行多列,充当的是数据源

eg: select * from student; --(改成表级子查询) select * from (select * from student) as stu_table;

猜你喜欢

转载自www.cnblogs.com/feixiaofei/p/12381551.html
今日推荐