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
//增
insert into student(name, age, stu_num, class)
value ('卡卡西',28,6600,1);

//改
update student set name='鸣人' where id = 1;
update student set name = '佐助', age=18, stu_num=6631, class=1 where id = 2;
update student
set id = 3, age=18, stu_num=6632, class=1
where name='小樱';

//查
select * from student; /*查询表中所有人*/

select name,age from student; /*查询表中所有人的名字和年龄*/

select * from student where age = 18; /*查询年龄为18的所有人*/

select count(1) from student; /*查询表中所有记录条数(在这里就是表中所有人总数)*/

select count(1) from student where age=18; /*查询表中年龄为18的人数*/

select sum(age) from student; /* 所有人的年龄加起来的总数*/

select avg(age) from student;/*求所有人的平均年龄*/

select avg(age) as avg_age from student; /*给平均数设置别名*/

select class, count(1) from student group by class; /*查询每班各有多少人,使用了group by进行分组*/

select name, class from student where age!=18 and class=3; /* 多个条件使用and */
select name, class from student where age=18 or stu_num>6640; /* 或操作使用or */

select * from student limit 2; /* 只取前两条记录*/

select * from student limit 5,3; /* 前5条记录不看,从第6条记录看起,取出3条记录*/

select * from student order by id; /* 以id进行正序排列 */
select * from student order by id desc ; /* 倒序排列 */


//删
delete from student; /*删除整个表。危险!!!*/

delete from student where name='卡卡西';