-
Notifications
You must be signed in to change notification settings - Fork 2
/
mysql基础语句总结
114 lines (114 loc) · 5.89 KB
/
mysql基础语句总结
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
** mysql基础语句总结**
```
显示数据库
show databases;
创建一个名为mydb1的数据库:
create database mydb1( if not exists);
创建一个使用UTF8字符集的mydb2的数据库(注意不是UTF-8):
create database if not exists mydb2 character set UTF8;
删除数据库mydb1
drop databese (if exists)mydb1;
显示刚才创建的数据库mydb1的定义信息
show create database mybd2;
使用数据库mydb2
use mydb2;
显示数据库中的表
show tables;
创建一张表student_info ,其中包含id,name,age,sex,score五个字段:
create table table student_info(id int,name varchar(20),age int,sex varchar(8),score int);
查看表student_info 的结构:
desc student_info;
向表student_info 中插入数据:
若不写字段内容,则只能按照字段一一对应插入:
insert into student_info values(1,'王二',22,'男',80);
也可以只给对应字段插入数据,其他缺省:
insert into student_info (name,age,sex) values('李依',23,'女');
修改表:
修改表中score列名为math
alter table student_info change column score math int;
删除表中math列
alter table student_info drop column math;
向表中增加列math_score、english_score、chinese_score;
alter table student_info add math_score/english_score/chinese_score int;
把表中name列的长度修改为50
alter table student_info modify name varchar(50);
修改表的字符集
alter table student_info character set UTF8/GBK/GB2312;
将所有学生的register_time都设为2017:
update student_info set register_time=2017;
加条件修改,where关键字(where 后若有多项则应用括号把每一项括起来,一项则可省略括号):
将学生李四的英语成绩修改为70:
update student_info set english_score=70 where name ='李四';
将张三的数学成绩加10分:
update student_info set math_score+=10 where name ='张三';
我们可以看出alter和update都是修改表信息,但是alter是对表结构的修改,而update是对表中内容的修改。
查询表:
查询所有学生信息
select * from student_info;(如果知道所有列名,也可将* 替换为所有列名,中间用逗号隔开,但比较麻烦,不推荐)
查询所有学生的语文成绩
select name,chinese_score from student_info;
过滤(distinct) 表中重复的年龄,即每个年龄的只保留一条:
select distinct age from student_info;
使用别名(别名要加上双引号):
select name "姓名",math_score+english_score+chinese_score "总分" from student_info;
范围(条件)查询,where关键字(加上not则表示不在该范围的查询):
查询表中name为张三的人的所有信息:
select * from student_info where name='张三';
查询表中name不为NULL的同学信息:
select * from student_info where name is not null;
查询数学成绩在80-90之间的同学信息:
①select * from student_info where (math_score >=80) and (math_score <= 90);
②select * from student_info where math_score (not) between 80 and 90;
查询英语成绩为85,90,和95的同学:
①select name,english_score from student_info where(english_score =85) or(english_score =90) or(english_score =95);
② select name,english_score from student_info where english_score (not) in(85,90,95);
模糊查找,like关键字:
查找所有姓’李‘的同学(%哪则模糊匹配哪部分):
select name from student_info where name like '李%';
查找所以名字中包含’李‘字的同学;
select name from student_info where name like '%李%';
查询表中所有记录也可写成:
select * from student_info where name like '%';(%也可以写多个,但没必要)
查询所有姓’李‘且名字为两个字的同学信息(后面有几个字符则几个下划线):
select * from student_info where name like '李_';
排序使用关键字 order by (排序字段用数值型 ,默认为升序):
降序 desc
select * from student_info order by math_score desc;
升序 asc
select * from student_info order by math_score asc;
总结:order by后面可以跟如下内容:
①字段
select * from student_info order by age desc;
②表达式
select * from student_info order by (math_score+english_score+chinese_score) desc;
③别名(别名不用加双引号)
select name "姓名",math_score+english_score+chinese_score "总分" from student_info order by 总分 desc;
④列号(按在select中的列的排序来显示,如下例中按总分降序排列)
select name, math_score+english_score+chinese_score from student_info order by 2 desc;
删除表中信息或表:
删除表中id为3的同学的所有信息:
delete from student_info where id=3;
删除表:
delete from student_info;//一行一行删,速度慢,删除后表结构还在,只不过内容没有了
drop table (if exists)student_info;//整个表删,速度快,删除后表不存在了
truncate table student_info;//删除表中内容后再重建表,删除后表结构还在,只不过内容没有了
一些统计函数:
count() 统计总数:
统计表中的总人数:
①select count(*) "总人数" from student_info;
②select count(id) "总人数" from student_info;
统计表中数学成绩大于80的人数:
select count(*) from student_info where (math_score >80);
sum() 求和:
求张三的总成绩:
select sum(math_score+english_score+chinese) from student_info where (name ='张三');
avg() 求平均值:
求全班学生的数学平均分:
select avg(math_score) from student_info;
求全班同学的总平均分:
select avg(math_score+english_score+chinese ) from student_info;
select avg(math_score+english_score+chinese ) from student_info where (name='张三');注意 这种写法是不对的,不能求出张三的平均分,因为math_score+english_score+chineses是一整项,而不是三个单独的成绩。
max() ,min() 求最值:
筛选班级里数学成绩最高和最低的同学:
select name,max(math_score),min(math_score) from student_info;
```