Getting Started with Data Analysis (2) - sql statement

1. The common data types

Integer: int, bit, tinyint (-128-127 range signed, unsigned range: 0-255)

Decimal: decimal (represented by floating-point numbers), such as a decimal (5,2) indicates a coexistence 5 digit decimal accounting 2

String: varchar (variable length), char

Date Time: date (xx years --xx month --xx day), time, datetime (xx years when --xx month --xx Day: min: sec)

Enumerated type: enum

Note : String text representation to store large text, when the character is greater than the recommended 4000

2. The common database operations

1. Connect database

Connect to the database in 1.cmd

mysql -u root (account) -p password

QQ screenshot 20200229091746.jpg

2. Direct start mysq client

Start -> Output mysql-> start my sql client

2. Exit Database

exit

3. What are the database display

show databases;

4. Check the database currently in use

select database();

5. Using a database

use database name;

6. Display Database Version

select version()

7 shows the current time

select now()

8. Create a database

create database + database name;

create database + database name charset = utf8; - specifies the character set format to utf8

9. Review the creation of a database statement

show create database + database name;

10. Delete Database

drop database demo;

11. Search functions

? function;

12. Backup Database

mysqldump -u root -p password + database name> .sql name

Note: The first to exit the mysql connection

13 recovery database

mysql -uroot -p password + database name <sql file

Table 3. Data Operation

1. Review all current data tables in the database

show tables;

2. Create a table

Syntax: Create Data Sheet + Table name (field name field type field constraints)

eg: create table student(

id int unsigned primary key auto_increment, - adding a field id, unsigned integer, primary keys, auto increment

name varchar (6) not null, - the name is not empty

age tinyint unsigned default 0, --默认0

high decimal (5,2), - accounting for two decimal integer 3

gender enum ( 'male', 'female', 'secret') default 'secret' - Note : do not add the last one, No.

);

3. View the table created

show create table + table name;

4. Check the table structure

desc + table name;

The modified table structure alter + add / modify (modification field type) / change (modify the field name and type) / drog (deleted)

1. Add field does not exist

Syntax: ALTER table + Table Column name Type add / constraints

eg: alter table student add birthday datetime default "2020-2-16 11:11:11" - add a birthday field, datetime type

2. Modify the present field type

Syntax: ALTER modify table + Table Column name Type / Constraint

eg: alter table student modify birthday date default "2020-3-2"; - to modify the above birthday date field type

3. Modify the field name and type

Syntax: ALTER table change column + Table Name Type / Constraint

eg: alter table student change birthday bir date default "2020-3-2"; - modify the birthday field bir

4. Modify the table - Delete field

Syntax: the ALTER the Table drop + + table name field name;

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

6. Delete table

Syntax: drop the Table + table name

eg: drop table student; - delete student table

7.mysql comments in

Use - to annotate

4. Data add, delete, change, operation

A. Increase insert

1. Full row insertion: insert into table values ​​(value 1, value 2, ......)

Features: order value correspondence table and fields, insertions to insert a data field containing the value of all the

QQ screenshot 20200229091809.jpg

eg: insert into student values ​​(0, 'Bei', 22) - When the table contains three fields, so the full use of a column is inserted, to one correspondence with the table

Note:

1. Here the id field of the use of auto-increment, it is possible to use placeholders

2. placeholders: only the primary key to use, the above embodiment, id to the primary key, there are three forms placeholder 0, default, Null

2. Insert the specified column: insert into table (column 1, ......) values ​​(values ​​1, ......)

Features: and column correspond, for insertion in a column designated

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

3. Multi-row insert (bulk insert): insert into table name (column 1, ...) values ​​(values ​​1, ...), (a value of 1, ...), ...

Features: insert multiple pieces of data

eg:

1.insert into student values ​​(default, 'Zhu Pig', 22, 'M'), (default, 'Luban', 23, 'M');

2.insert into student (name, age, sex) values ​​( 'Zhou Yu', 23, 'M'), ( 'Yaojin', 25, 'M');

Second, delete update

1. Syntax: Update table set value = 1 column 1, column 2 value = 2, ... [WHERE conditions]

Note: you must place where conditions, otherwise it becomes a full table update

eg: update student set sex = 'M' where name = 'Guan';

Third, delete delete (physical deletion)

1. Grammar: the Delete from table [where] conditions

Note: you must place conditions, otherwise you will wait right foot (manual funny)

eg: delete from student where id = 1;

Fourth, select the query

1. query all the columns

Syntax: the SELECT * from table name +

eg:select * from student;

2. Specify the query field

Syntax: the SELECT column 1, column 2, ..... from table

eg: select name,age from student;

1. Use as aliases to field queries

eg: select name as the name, age as the age from student; --name name from the alias

Complete form of 2.sql ( To bring the table when doing multi-table queries )

eg: select demo.student.name, demo.student.age from student; --demo student table database, may be omitted

3. Use as an alias for the table to play

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

4. Use of the distinct eliminates duplicate rows

eg : select distinct sex from student;

Note: When there are multiple fields after distinct, only when the query results are identical multi-column query can go heavy

QQ screenshot 20200229091818.jpg

eg: select distinct sex,age from student;

QQ screenshot 20200229091826.jpg

3. Specify a query where condition

1. The comparison operators>, <,> =, <=,! =

eg:

1. Query student more than 18 years of age

select * from student where age > 18;

2. "does not mean" the two representations

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

2. The logical operators and (to), not (non), or (or)

eg:

1.select * from student where age> 18 and sex = 'female'; - Query more than 18 year-old girl

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 screenshot 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 screenshot 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 screenshot 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 screenshot 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) Column-level sub-query: the results of the sub-query is a query more than one row

eg: select * from student where age in (18,20,30) --- (into sub-queries) select * from student where age in (select age from student where age in 18,20,30); check the main query the students all the information, the sub-query is a query satisfy the age of 18, 20,

3) Row-level sub-queries: row-level sub-queries need to build the line elements

Row elements: a plurality of row elements combined into fields, row subquery row element will be used in

eg: query highest height of the oldest students in class

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

note: Here the age and height combined into one element row

4) Table-level sub-query: results of the query are rows and columns, to act as a data source

eg: select * from student; - (subquery into table level) select * from (select * from student) as stu_table;

Guess you like

Origin www.cnblogs.com/feixiaofei/p/12381551.html