【软件测试】mysql中的高级连接查询,详细教程


前言

于数据库,软件测试工程师第一个要学的技术也是最重要的基础。

不仅你做功能测试要用到数据库;
接口测试、很多接口的返回值它是动态的,那么你要去数据库拿数据来校验;
还有自动化,怎么去做一些数据驱动,都要从数据库里去拿。
你做性能测试是不是也和数据库有关。比如慢查询,都和数据库有关。

所以说,你要去面试软件测试工程师。数据库这一关你得要有底。

各种增删改查操作的语法很是头痛,后续多表查询中内外连接更是头懵,多表联查操作,实际就是在基础的查询语句里不同的位置跟子句进行查询,现以mysql为例总结一下:

存在学生信息表student:

select * from student;
+-----+-------+------+----------+------+
| sno | sname | sex  | saddress | sage |
+-----+-------+------+----------+------+
|   1 | 李四  || 上海     |   19 |
|   3 | 王五  || 湖北武汉 |   18 |
|   4 | 赵六  || 海南三亚 |   22 |
+-----+-------+------+----------+------+
3 rows in set (0.00 sec))

存在分数表score:

select * from score;
+----+------+------+-------+
| id | sid  | cid  | score |
+----+------+------+-------+
|  1 |    3 |    1 |    98 |
|  2 |    4 |    1 |    88 |
|  3 |    1 |    1 |    56 |
|  4 |    4 |    2 |    79 |
+----+------+------+-------+
4 rows in set (0.00 sec)

存在课程表course:

select * from course;
+----------+------------+
| courseid | coursename |
+----------+------------+
|        1 | mysql      |
|        2 | linux      |
+----------+------------+
2 rows in set (0.05 sec)

一、where条件后面的子句

1、等值连接

只返回两个表中连接字段相等的行

举例:查询每个参加了考试的学生信息

select * from score,student where score.sid=student.sno;
+----+------+------+-------+-----+-------+------+----------+
| id | sid  | cid  | score | sno | sname | sex  | saddress |
+----+------+------+-------+-----+-------+------+----------+
|  1 |    3 |    1 |    98 |   3 | 王五  || 湖北武汉 |
|  2 |    4 |    1 |    88 |   4 | 赵六  || 海南三亚 |
+----+------+------+-------+-----+-------+------+----------+
2 rows in set (0.00 sec)

2、非等值连接

做多表连接时使用非’=’号,而使用其它连接运算符的

存在表grade:

select * from grade;
+----+------+------+------+
| id | low  | high | rank |
+----+------+------+------+
|  1 |   80 |  100 | A    |
|  2 |   70 |   79 | B    |
|  3 |   60 |   69 | C    |
|  4 |    0 |   59 | D    |
+----+------+------+------+
4 rows in set (0.00 sec)

举例:查询每个分数的等级

select score,rank from score,grade where score.score between low and high;
+-------+------+
| score | rank |
+-------+------+
|    98 | A    |
|    88 | A    |
|    79 | B    |
+-------+------+
3 rows in set (0.00 sec)

3、自连接

把表虚拟成两个表,自身跟自身进行比较

举例:查询有哪些人比王五大

select b.sname from student a,student b where b.sage > a.sage and a.sname='王五';
+-------+
| sname |
+-------+
| 李四  |
| 赵六  |
+-------+
2 rows in set (0.00 sec)

4、左连接

外连接中的一种,会显示左表中的所有数据及右表满足条件的数据
举例:查询所有学生的考试情况

select * from student left join score on student.sno=score.sid;
+-----+-------+------+----------+------+------+------+------+-------+
| sno | sname | sex  | saddress | sage | id   | sid  | cid  | score |
+-----+-------+------+----------+------+------+------+------+-------+
|   3 | 王五  || 湖北武汉 |   18 |    1 |    3 |    1 |    98 |
|   4 | 赵六  || 海南三亚 |   22 |    2 |    4 |    1 |    88 |
|   4 | 赵六  || 海南三亚 |   22 |    4 |    4 |    2 |    79 |
|   1 | 李四  || 上海     |   19 | NULL | NULL | NULL |  NULL |
+-----+-------+------+----------+------+------+------+------+-------+
4 rows in set (0.01 sec

5、右连接

外连接中的一表,会显示右表中所有的数据及左表满足条件的数据

举例:查询参数了考试的学生信息

select * from student right join score on student.sno=score.sid;
+------+-------+------+----------+------+----+------+------+-------+
| sno  | sname | sex  | saddress | sage | id | sid  | cid  | score |
+------+-------+------+----------+------+----+------+------+-------+
|    3 | 王五  || 湖北武汉 |   18 |  1 |    3 |    1 |    98 |
|    4 | 赵六  || 海南三亚 |   22 |  2 |    4 |    1 |    88 |
|    4 | 赵六  || 海南三亚 |   22 |  4 |    4 |    2 |    79 |
+------+-------+------+----------+------+----+------+------+-------+
3 rows in set (0.00 sec)

6、子查询

当一个查询是另一个查询的条件时,称之为子查询
举例:查询比王五大的学生

select sname from student where sage > (select sage from student where sname='王五');
+-------+
| sname |
+-------+
| 李四  |
| 赵六  |
+-------+
2 rows in set (0.00 sec)

二、from后面的子句

派生表:按条件查询结果并派生成临时表,可供查询,但一定话题要带别名
举例:选修了mysql这门课的学生有哪些?

select * from (select * from score ,course where score.cid=course.courseid and course.coursename='mysql')b ,student a where b.sid = a.sno;
+----+------+------+-------+----------+------------+-----+-------+------+----------+------+
| id | sid  | cid  | score | courseid | coursename | sno | sname | sex  | saddress | sage |
+----+------+------+-------+----------+------------+-----+-------+------+----------+------+
|  1 |    3 |    1 |    98 |        1 | mysql      |   3 | 王五  || 湖北武汉 |   18 |
|  2 |    4 |    1 |    88 |        1 | mysql      |   4 | 赵六  || 海南三亚 |   22 |
+----+------+------+-------+----------+------------+-----+-------+------+----------+------+
2 rows in set (0.00 sec)

三、select后面的子句

举例:计算mysql这门课中的及格率

select (select count(*) from score,course where coursename='mysql' and score >=60)/(select count(*) from score,course where coursename='mysql')*100 '及格率';
+---------+
| 及格率  |
+---------+
| 75.0000 |
+---------+
1 row in set (0.00 sec)

四、总结

奋斗是这么个过程,当时不觉累,事后不会悔。走一段再回头,会发现一个更强的自己,宛如新生。

梦想是注定孤独的旅行,路上少不了质疑和嘲笑,但那又怎样,哪怕遍体鳞伤也要活的漂亮。

成人的世界,天黑可以矫情,天亮就要拼命,岁月不会安逸,无论你当下经历了什么,天亮以后依然踏着荆棘前行!

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 18
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值