• 软件测试工程师必备的SQL语句基础


    为一个软件测试工程师,我们在测试过程中往往需要对数据库数据进行操作,但是我们的操作大多以查询居多,有时会涉及到新增,修改,删除等操作,所以我们其实并不需要对数据库的操作有特别深入的了解,以下是我在工作过程中整理的比较常用的SQL语句

    1.插入表数据:

    insert into 表名1 (字段1,字段2) values(字段1值,字段2值);

    2.删除表数据:

    delete:delete from 表名1 where 范围(删除表内符合条件的内容)

    delete from 表名1(清空数据表内容,不释放空间,即:下次插入表数据,id依然接着删除数据的id继续增加)

    truncate:truncate table 表名1(清空表数据,释放空间,即:下次插入表数据,id从1重新开始)

    drop:drop table 表名1(整张表被删除,要使用该表必须重新建)

    3.修改表数据:

    update 表名1 set 字段名 = ‘新值’ where 范围

    4.查询表数据:

    查询数据:select * from table1 where 范围

    总数:select count (*) from table1 where 范围

    select count (distinct(字段1) from table1 where 范围(distinct可去重)

    求和:select sum (字段1) from table1 where 范围

    平均:select avg (字段1) from table1 where 范围

    最大:select max (字段1) from table1 where 范围

    最小:select min (字段1) from table1 where 范围

    排序:select * from table1 where 范围 order by 排序字段名 desc(desc逆序排序。默认是正序排序asc)

    5.复杂查询:

    嵌套查询:多个查询语句嵌套在一起查询,一般嵌套的查询语句放在where 或 having 的后面

    例:

    select * from table1 where status in(select status from table2)

    多表连接查询:

    table1:

    id   username
    1张三
    2李四
    3

    王二

    table2:

    idjob
    1teacher
    2student
    4worker

    (1)内联查询(inner join……on……)

    select * from table1 a inner join table2 b on a.id=b.id

    查询结果:

    id

    username

    id

    job

    1

    张三

    1

    teacher

    2

    李四

    2

    student

    (2)左外联(left outer join……on……)

    select * from table1 a left outer join table2 b on a.id=b.id

    查询结果

    id

    username

    id

    job

    1

    张三

    1

    teacher

    2

    李四

    2

    student

    3

    王二

    null

    null

    (3)右外联(right outer join……on……)

    select * from table1 a right outer join table2 b on a.id=b.id

    id

    username

    id

    job

    1

    张三

    1

    teacher

    2

    李四

    2

    student

    null

    null

    4

    worker

    (4)全外联(full outer join……on……)

    select * from table1 a full outer join table2 b on a.id=b.id

    id

    username

    id

    job

    1

    张三

    1

    teacher

    2

    李四

    2

    student

    3

    王二

    null

    null

    null

    null

    4

    worker

    6.group by分组

    根据某一个或多个列表字段进行分组统计。

    table1:

    id

    name

    course

    score

    1

    张一

    Chinese

    80

    2

    张二

    Chinese

    60

    3

    张三

    math

    65

    4

    张三

    Chinese

    70

    5

    张一

    math

    90

    查询每个用户的最高成绩:

    select name,max(score) as max_score from table1 group by name

    查询结果:先按用户名分组,再在每个组中查询找到最高分数

    id

    name

    max_score

    1

    张一

    90

    2

    张二

    60

    3

    张三

    70

    查询全班每科课程平均分

    select course,avg(score) as avg_score from table1 group by course

    查询结果:先按课程分组,再在每个组中查询找到平均分数

    id

    course

    avg_score

    1

    chinese

    70

    2

    math

    77.5

    having的用法:同where用法,having与group by连用。where是筛选单个记录,having是筛选分组记录(先分组,后筛选)

    作为一个初中级测试人员,一般情况下拥有以上的数据库知识就可以满足大部分的测试需要了。

    最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

    在这里插入图片描述

    这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

  • 相关阅读:
    CF770C Online Courses In BSU
    PaddleSeg 自定义数据类
    Vue使用CryptoJS实现前后端密码加密
    4.1 数据库安全性概述
    3559. 围圈报数
    Win11打不开exe应用程序怎么办?Win11无法打开exe程序解决方法
    linux用户及密码的存储和校验方法
    JAVA语言特性
    Mybatis-Plus之单表操作和分表查询
    day06_面向对象基础
  • 原文地址:https://blog.csdn.net/NHB456789/article/details/133810198