1、sql 新增语句
mysql> insert into t_student values(1,"zhangsan",18,"boy"),(2,"wangwu",20,"girl");
mysql> select * from t_student;
+----+----------+------+------+
| id | name | age | sex |
+----+----------+------+------+
| 1 | zhangsan | 18 | boy |
| 2 | wangwu | 20 | girl |
+----+----------+------+------+
mysql> insert into t_student(id,name,age,sex)values(3,"zhaoliu",19,"boy");
mysql> select * from t_student;
+----+----------+------+------+
| id | name | age | sex |
+----+----------+------+------+
| 1 | zhangsan | 18 | boy |
| 2 | wangwu | 20 | girl |
| 3 | zhaoliu | 19 | boy |
+----+----------+------+------+
# 删除 t_student 表中 id = 4 的数据
mysql> delete from t_student where id = 4
# 清空 t_student 表
mysql> delete from t_student
# 修改 t_student 表中 wangwu 的性别为 boy
mysql> update t_student set sex = "boy" where name = "wangwu";
mysql> select * from t_student;
+----+----------+------+------+
| id | name | age | sex |
+----+----------+------+------+
| 1 | zhangsan | 18 | boy |
| 2 | wangwu | 20 | boy |
| 3 | zhaoliu | 19 | boy |
+----+----------+------+------+
4、sql 单表查询语句
# 查询 t_student 表中所有数据
mysql> select * from t_student;
+----+----------+------+------+
| id | name | age | sex |
+----+----------+------+------+
| 1 | zhangsan | 18 | boy |
| 2 | wangwu | 20 | boy |
| 3 | zhaoliu | 19 | boy |
+----+----------+------+------+
# 查询出 t_student 表中 name 字段的所有数据
mysql> select name from t_student;
+----------+
| name |
+----------+
| zhangsan |
| wangwu |
| zhaoliu |
+----------+
# 查询出 t_student 表中 name,age 两个字段的所有数据
mysql> select name,age from t_student;
+----------+------+
| name | age |
+----------+------+
| zhangsan | 18 |
| wangwu | 20 |
| zhaoliu | 19 |
+----------+------+
mysql> select age,sex from t_student where name = "zhaoliu";
+------+------+
| age | sex |
+------+------+
| 19 | boy |
+------+------+
mysql> select age,sex from t_student where name != "zhaoliu";
+------+------+
| age | sex |
+------+------+
| 18 | boy |
| 20 | boy |
+------+------+
mysql> select age,sex from t_student where name <> "zhaoliu";
+------+------+
| age | sex |
+------+------+
| 18 | boy |
| 20 | boy |
+------+------+
# 查询出性别为 boy 且班级为 2 班的学生信息
mysql> select * from t_student where sex = "boy" and class = 2;
+----+---------+------+------+-------+
| id | name | age | sex | class |
+----+---------+------+------+-------+
| 3 | zhaoliu | 19 | boy | 2 |
+----+---------+------+------+-------+
# 查询出年纪为 18 岁 或者班级为 2 班的学生信息
mysql> select * from t_student where age = 18 or class = 2;
+----+----------+------+------+-------+
| id | name | age | sex | class |
+----+----------+------+------+-------+
| 1 | zhangsan | 18 | boy | 1 |
| 3 | zhaoliu | 19 | boy | 2 |
+----+----------+------+------+-------+
# 查询出年纪在 19 - 20 之间的学生信息
mysql> select * from t_student where age between 19 and 20;
+----+---------+------+------+-------+
| id | name | age | sex | class |
+----+---------+------+------+-------+
| 2 | wangwu | 20 | boy | 1 |
| 3 | zhaoliu | 19 | boy | 2 |
+----+---------+------+------+-------+
mysql> select * from t_student where age in (18,19);
+----+----------+------+------+-------+
| id | name | age | sex | class |
+----+----------+------+------+-------+
| 1 | zhangsan | 18 | boy | 1 |
| 3 | zhaoliu | 19 | boy | 2 |
+----+----------+------+------+-------+
mysql> select * from t_student where age not in (18,19);
+----+--------+------+------+-------+
| id | name | age | sex | class |
+----+--------+------+------+-------+
| 2 | wangwu | 20 | boy | 1 |
+----+--------+------+------+-------+
mysql> select * from t_student where class is NULL;
+----+------+------+------+-------+
| id | name | age | sex | class |
+----+------+------+------+-------+
| 4 | lisi | 22 | girl | NULL |
+----+------+------+------+-------+
mysql> select * from t_student where class is not NULL;
+----+----------+------+------+-------+
| id | name | age | sex | class |
+----+----------+------+------+-------+
| 1 | zhangsan | 18 | boy | 1 |
| 2 | wangwu | 20 | boy | 1 |
| 3 | zhaoliu | 19 | boy | 2 |
+----+----------+------+------+-------+
# 查询出表中姓 zhang 的学生信息
mysql> select * from t_student where name like "zhang%";
+----+----------+------+------+-------+
| id | name | age | sex | class |
+----+----------+------+------+-------+
| 1 | zhangsan | 18 | boy | 1 |
+----+----------+------+------+-------+
# 查询出表中姓名中带有 ng 的学生信息
mysql> select * from t_student where name like "%ng%";
+----+----------+------+------+-------+
| id | name | age | sex | class |
+----+----------+------+------+-------+
| 1 | zhangsan | 18 | boy | 1 |
| 2 | wangwu | 20 | boy | 1 |
+----+----------+------+------+-------+
# 查询出表中 第 2,3 两行学生信息
mysql> select * from t_student limit 1,2;
+----+---------+------+------+-------+
| id | name | age | sex | class |
+----+---------+------+------+-------+
| 2 | wangwu | 20 | boy | 1 |
| 3 | zhaoliu | 19 | boy | 2 |
+----+---------+------+------+-------+
# 查询表数据,按照 age 字段升序排序
mysql> select * from t_student order by age asc;
+----+----------+------+------+-------+
| id | name | age | sex | class |
+----+----------+------+------+-------+
| 1 | zhangsan | 18 | boy | 1 |
| 3 | zhaoliu | 19 | boy | 2 |
| 2 | wangwu | 20 | boy | 1 |
| 4 | lisi | 22 | girl | NULL |
+----+----------+------+------+-------+
# 查询表数据,按照 age 字段降序排序
mysql> select * from t_student order by age desc;
+----+----------+------+------+-------+
| id | name | age | sex | class |
+----+----------+------+------+-------+
| 4 | lisi | 22 | girl | NULL |
| 2 | wangwu | 20 | boy | 1 |
| 3 | zhaoliu | 19 | boy | 2 |
| 1 | zhangsan | 18 | boy | 1 |
+----+----------+------+------+-------+
mysql> select * from t_student group by class;
+----+----------+------+------+-------+
| id | name | age | sex | class |
+----+----------+------+------+-------+
| 4 | lisi | 22 | girl | NULL |
| 1 | zhangsan | 18 | boy | 1 |
| 3 | zhaoliu | 19 | boy | 2 |
+----+----------+------+------+-------+
mysql> select * from t_student group by class having sex = "girl";
+----+------+------+------+-------+
| id | name | age | sex | class |
+----+------+------+------+-------+
| 4 | lisi | 22 | girl | NULL |
+----+------+------+------+-------+
# 统计表中有多少行数据
mysql> select count(*) from t_student;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
# 求出所有学员年纪之和
mysql> select sum(age) from t_student;
+----------+
| sum(age) |
+----------+
| 79 |
+----------+
# 求出平均年纪
mysql> select avg(age) from t_student;
+----------+
| avg(age) |
+----------+
| 19.7500 |
+----------+
# 求出最大年纪
mysql> select max(age) from t_student;
+----------+
| max(age) |
+----------+
| 22 |
+----------+
# 求出最小年纪
mysql> select min(age) from t_student;
+----------+
| min(age) |
+----------+
| 18 |
+----------+
# 对 class 字段去重
mysql> select distinct(class) from t_student;
+-------+
| class |
+-------+
| 1 |
| 2 |
| NULL |
+-------+