MySql的复习(三)

本文详细介绍了MySQL中的基本查询语句,包括查询所有数据、指定条件查询、使用别名、多条件查询、范围查询、模糊查询以及排序。此外,还讲解了分组和聚合函数的应用,如COUNT、SUM、AVG、MIN、MAX,并展示了分页查询的用法。这些内容构成了数据库操作的基础,对于理解和掌握SQL语言至关重要。
摘要由CSDN通过智能技术生成

数据库sql语句(DRL)

1、查询所有数据

  • selsect * from 表名
* mysql> select * from student;
+----+--------+-----+-----+------+-------+-------------+-----+
| id | sname  | sex | age | team | score | phone       | fun |
+----+--------+-----+-----+------+-------+-------------+-----+
|  1 | 张三   ||  22 | 学生 | 95.00 | 17777118877 |   0 |
|  3 | 李四   ||  21 | 学生 | 88.00 | 17777119988 |   1 |
|  4 | 王五   ||  20 | 学生 | 99.00 | 17777112244 |   1 |
|  5 | 赵六   ||  23 | 学生 | 59.00 | 17777114411 |   1 |
|  6 | 大黄   ||  22 | 学生 | 77.00 | 17777114444 |   3 |
|  7 | 李华   ||  25 | 学生 | 49.00 | 17777113333 |   4 |
|  8 | 孙笑川 ||  26 | 学生 | 60.00 | 17777113314 |   5 |
+----+--------+-----+-----+------+-------+-------------+-----+
7 rows in set

指定条件查询

  • select * from 表名 where 条件;
mysql> select * from student where id=3;
+----+-------+-----+-----+------+-------+-------------+-----+
| id | sname | sex | age | team | score | phone       | fun |
+----+-------+-----+-----+------+-------+-------------+-----+
|  3 | 李四  ||  21 | 学生 | 88.00 | 17777119988 |   1 |
+----+-------+-----+-----+------+-------+-------------+-----+
1 row in set

mysql> select * from student where id=4
;
+----+-------+-----+-----+------+-------+-------------+-----+
| id | sname | sex | age | team | score | phone       | fun |
+----+-------+-----+-----+------+-------+-------------+-----+
|  4 | 王五  ||  20 | 学生 | 99.00 | 17777112244 |   1 |
+----+-------+-----+-----+------+-------+-------------+-----+
1 row in set
  • 查询字段为空的信息记录
select * from student where sex is null;
  • 查询指定字段的数据记录
mysql> select sname,sex from student;
+--------+-----+
| sname  | sex |
+--------+-----+
| 张三   ||
| 李四   ||
| 王五   ||
| 赵六   ||
| 大黄   ||
| 李华   ||
| 孙笑川 ||
+--------+-----+
7 rows in set

mysql> select sname,score
 from student;
+--------+-------+
| sname  | score |
+--------+-------+
| 张三   | 95.00 |
| 李四   | 88.00 |
| 王五   | 99.00 |
| 赵六   | 59.00 |
| 大黄   | 77.00 |
| 李华   | 49.00 |
| 孙笑川 | 60.00 |
+--------+-------+
7 rows in set
  • 使用别名,好处:在多表中进行区分 ,整洁
mysql> select s.sname,s.age from student s;
+--------+-----+
| sname  | age |
+--------+-----+
| 张三   |  22 |
| 李四   |  21 |
| 王五   |  20 |
| 赵六   |  23 |
| 大黄   |  22 |
| 李华   |  25 |
| 孙笑川 |  26 |
+--------+-----+
7 rows in set
  • 多个条件查询 and(与),or(或)
mysql> select * from student where age = 22 and sex = '女';
+----+-------+-----+-----+------+-------+-------------+-----+
| id | sname | sex | age | team | score | phone       | fun |
+----+-------+-----+-----+------+-------+-------------+-----+
|  6 | 大黄  ||  22 | 学生 | 77.00 | 17777114444 |   3 |
+----+-------+-----+-----+------+-------+-------------+-----+
1 row in set

mysql> select * from student where age = 22 or sex = '女';
+----+-------+-----+-----+------+-------+-------------+-----+
| id | sname | sex | age | team | score | phone       | fun |
+----+-------+-----+-----+------+-------+-------------+-----+
|  1 | 张三  ||  22 | 学生 | 95.00 | 17777118877 |   0 |
|  4 | 王五  ||  20 | 学生 | 99.00 | 17777112244 |   1 |
|  6 | 大黄  ||  22 | 学生 | 77.00 | 17777114444 |   3 |
+----+-------+-----+-----+------+-------+-------------+-----+
3 rows in set
  • 查询数据的时,指定条件的范围性
mysql> select * from student where sex='男' and score>90;
+----+-------+-----+-----+------+-------+-------------+-----+
| id | sname | sex | age | team | score | phone       | fun |
+----+-------+-----+-----+------+-------+-------------+-----+
|  1 | 张三  ||  22 | 学生 | 95.00 | 17777118877 |   0 |
+----+-------+-----+-----+------+-------+-------------+-----+
1 row in set

mysql> select * from student where  score<80;
+----+--------+-----+-----+------+-------+-------------+-----+
| id | sname  | sex | age | team | score | phone       | fun |
+----+--------+-----+-----+------+-------+-------------+-----+
|  5 | 赵六   ||  23 | 学生 | 59.00 | 17777114411 |   1 |
|  6 | 大黄   ||  22 | 学生 | 77.00 | 17777114444 |   3 |
|  7 | 李华   ||  25 | 学生 | 49.00 | 17777113333 |   4 |
|  8 | 孙笑川 ||  26 | 学生 | 60.00 | 17777113314 |   5 |
+----+--------+-----+-----+------+-------+-------------+-----+
4 rows in set
  • 模糊查询 like ,‘T%’ 首位字符 ,‘%T’ 末尾字符 ,‘%T%’ 包含字符 ‘_’ 代表了一个字符
mysql> select * from student where sname like '大_';
+----+-------+-----+-----+------+-------+-------------+-----+
| id | sname | sex | age | team | score | phone       | fun |
+----+-------+-----+-----+------+-------+-------------+-----+
|  6 | 大黄  ||  22 | 学生 | 77.00 | 17777114444 |   3 |
+----+-------+-----+-----+------+-------+-------------+-----+
1 row in set

mysql> select * from student where sname like '孙_%';
+----+--------+-----+-----+------+-------+-------------+-----+
| id | sname  | sex | age | team | score | phone       | fun |
+----+--------+-----+-----+------+-------+-------------+-----+
|  8 | 孙笑川 ||  26 | 学生 | 60.00 | 17777113314 |   5 |
+----+--------+-----+-----+------+-------+-------------+-----+
1 row in set

排序

  • 查询同时进行排序 order by 字段 desc (降序) , order by 字段 asc(升序 默认)
mysql> select * from student order by score;
+----+--------+-----+-----+------+-------+-------------+-----+
| id | sname  | sex | age | team | score | phone       | fun |
+----+--------+-----+-----+------+-------+-------------+-----+
|  7 | 李华   ||  25 | 学生 | 49.00 | 17777113333 |   4 |
|  5 | 赵六   ||  23 | 学生 | 59.00 | 17777114411 |   1 |
|  8 | 孙笑川 ||  26 | 学生 | 60.00 | 17777113314 |   5 |
|  6 | 大黄   ||  22 | 学生 | 77.00 | 17777114444 |   3 |
|  3 | 李四   ||  21 | 学生 | 88.00 | 17777119988 |   1 |
|  1 | 张三   ||  22 | 学生 | 95.00 | 17777118877 |   0 |
|  4 | 王五   ||  20 | 学生 | 99.00 | 17777112244 |   1 |
+----+--------+-----+-----+------+-------+-------------+-----+
7 rows in set

mysql> select * from student order by score desc
;
+----+--------+-----+-----+------+-------+-------------+-----+
| id | sname  | sex | age | team | score | phone       | fun |
+----+--------+-----+-----+------+-------+-------------+-----+
|  4 | 王五   ||  20 | 学生 | 99.00 | 17777112244 |   1 |
|  1 | 张三   ||  22 | 学生 | 95.00 | 17777118877 |   0 |
|  3 | 李四   ||  21 | 学生 | 88.00 | 17777119988 |   1 |
|  6 | 大黄   ||  22 | 学生 | 77.00 | 17777114444 |   3 |
|  8 | 孙笑川 ||  26 | 学生 | 60.00 | 17777113314 |   5 |
|  5 | 赵六   ||  23 | 学生 | 59.00 | 17777114411 |   1 |
|  7 | 李华   ||  25 | 学生 | 49.00 | 17777113333 |   4 |
+----+--------+-----+-----+------+-------+-------------+-----+
7 rows in set

mysql> select * from student order by score asc;
+----+--------+-----+-----+------+-------+-------------+-----+
| id | sname  | sex | age | team | score | phone       | fun |
+----+--------+-----+-----+------+-------+-------------+-----+
|  7 | 李华   ||  25 | 学生 | 49.00 | 17777113333 |   4 |
|  5 | 赵六   ||  23 | 学生 | 59.00 | 17777114411 |   1 |
|  8 | 孙笑川 ||  26 | 学生 | 60.00 | 17777113314 |   5 |
|  6 | 大黄   ||  22 | 学生 | 77.00 | 17777114444 |   3 |
|  3 | 李四   ||  21 | 学生 | 88.00 | 17777119988 |   1 |
|  1 | 张三   ||  22 | 学生 | 95.00 | 17777118877 |   0 |
|  4 | 王五   ||  20 | 学生 | 99.00 | 17777112244 |   1 |
+----+--------+-----+-----+------+-------+-------------+-----+
7 rows in set

2、分组,聚合函数

  • 相关的语句有:
  • group by ,having 分组
  • count() 返回指定条件的行数
  • sum() 求和
  • avg() 平均值
  • min(),max() 最小最大值

使用例子

  • 查询条数 count(*)
mysql> select count(*) 人数 from student;
+------+
| 人数 |
+------+
|    7 |
+------+
1 row in set
  • 根据条件分组查询
mysql> select count(*) 'fun=1的人数' from student group by fun having fun='1';
+-------------+
| fun=1的人数 |
+-------------+
|           3 |
+-------------+
1 row in set
  • 求和查询
mysql> select sum(age) from student;
+----------+
| sum(age) |
+----------+
| 159      |
+----------+
1 row in set
  • 平均值查询
mysql> select avg(age) 年龄,avg(score) 成绩 from student;
+---------+-----------+
| 年龄    | 成绩      |
+---------+-----------+
| 22.7143 | 75.285714 |
+---------+-----------+
1 row in set
  • 最大最小值
mysql> select min(age) 最小年龄 from student;
+----------+
| 最小年龄 |
+----------+
|       20 |
+----------+
1 row in set
mysql> select max(age) 最大年龄 from student;
+----------+
| 最大年龄 |
+----------+
|       26 |
+----------+
1 row in set

4 、分页查询

  • limit i, n i 初始位置 (查询的是索引值,从0开始) n 总数
    如下:limit 2,3 表示从索引2开始,即从第3条记录开始,查询一共3条记录
mysql> select * from student;
+----+--------+-----+-----+------+-------+-------------+-----+
| id | sname  | sex | age | team | score | phone       | fun |
+----+--------+-----+-----+------+-------+-------------+-----+
|  1 | 张三   ||  22 | 学生 | 95.00 | 17777118877 |   0 |
|  3 | 李四   ||  21 | 学生 | 88.00 | 17777119988 |   1 |
|  4 | 王五   ||  20 | 学生 | 99.00 | 17777112244 |   1 |
|  5 | 赵六   ||  23 | 学生 | 59.00 | 17777114411 |   1 |
|  6 | 大黄   ||  22 | 学生 | 77.00 | 17777114444 |   3 |
|  7 | 李华   ||  25 | 学生 | 49.00 | 17777113333 |   4 |
|  8 | 孙笑川 ||  26 | 学生 | 60.00 | 17777113314 |   5 |
+----+--------+-----+-----+------+-------+-------------+-----+
7 rows in set

mysql> select * from student limit 0,3;
+----+-------+-----+-----+------+-------+-------------+-----+
| id | sname | sex | age | team | score | phone       | fun |
+----+-------+-----+-----+------+-------+-------------+-----+
|  1 | 张三  ||  22 | 学生 | 95.00 | 17777118877 |   0 |
|  3 | 李四  ||  21 | 学生 | 88.00 | 17777119988 |   1 |
|  4 | 王五  ||  20 | 学生 | 99.00 | 17777112244 |   1 |
+----+-------+-----+-----+------+-------+-------------+-----+
3 rows in set

mysql> select * from student limit 2,3;
+----+-------+-----+-----+------+-------+-------------+-----+
| id | sname | sex | age | team | score | phone       | fun |
+----+-------+-----+-----+------+-------+-------------+-----+
|  4 | 王五  ||  20 | 学生 | 99.00 | 17777112244 |   1 |
|  5 | 赵六  ||  23 | 学生 | 59.00 | 17777114411 |   1 |
|  6 | 大黄  ||  22 | 学生 | 77.00 | 17777114444 |   3 |
+----+-------+-----+-----+------+-------+-------------+-----+
3 rows in set
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值