• 测试人需要的数据库知识:MySQL常用语法


    作为一名测试人,掌握数据库常用的操作不可缺少,下面就一段MySQL 查询语句梳理几个相关语法知识。

    本篇文章主要包括两方面内容:

    常用的sql语法

    开发代码中如何和sql进行关联

    案例代码:

    
    `
    select a.blastid,d.apptime,a.charge_approve,
    
                a.blast_batch,
    
                a.plan_blast_time,
    
                a.exe_plan_id,
    
                a.import_flag,
    
                a.rig_type,.......
    
                case when (select "count"(*) from bla_geology_info geo where a.blast_id=geo.blast_id and  geo.delete_flag=0)>0
    
                    then 'true' else 'false'  
    
        END as isShowFlag
    
        from bla_blast_info a
    
        left join exe_plan_month b
    
        on a.exe_plan_id = b.exe_plan_id
    
        left join sys_user c
    
        on a.dynamite_user = c.user_id
    
        left join (select blast_id,isagree,idea,create_time as app_time from bla_charge_approve where delete_flag = '0') d
    
        on a.blast_id = d.blast_id
    
        WHERE a.blast_type = '1' AND a.blast_status > '3' AND a.blast_status < '8' AND a.charge_approve > '1' AND a.delete_flag = '0' ORDER BY cast(a.charge_approve  as numeric)  ASC , d.app_time DESC , a.create_time DESC
    `
    
    • 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

    常用的sql语法

    1

    查询语句 select xxxx from 表名 ,表示从哪个表进行查询。

    其中select 后面内容以“,”分割。

    2

    select xxxx from 表名 别名,表示这个表用别名替代,如上个语句中的a、b、c等。

    3

    case when then xx,else xxx ,end as isShowFlag表示符合 when 条件执行 then 后面语句,否则执行 else 后面语句。

    eg 案例中case when (select “count”(*) from blageologyinfo geo where a.blastid=geo.blastid and geo.delete_flag=0)>0

    then ‘true’ else ‘false’ 表示符合条件的爆破量进行上传

    end as isShowFlag 起的查询语句别名,后续调用的时候使用别名名称即可。

    4

    left join 左连接 ,表1左连接表2,以左为主,表示以表1为主,关联上表2的数据,查出来的结果显示左边的所有数据,然后右边显示的是和左边有交集部分的数据。

    在这里插入图片描述
    一句话的意思是:先取交集,再关联到左表。

    引申整理出几个连接的常用含义及区分 ,如下表所示:

    在这里插入图片描述

    5

    查询语句 select xxxx from 表名 where 查询条件。

    where 查询条件后面接所有你要查询的内容,用and连接,这里需要注意order by 使用

    order by 1,2,3,4 ,先1排序,1相同的情况下按照2、3、4排序。

    6

    cast ()数据类型转换函数,文中cast(a.charge_approve as numeric) 表示将字段charge_approve 转换为数字型,以前是字符类型,字符类型的字段不可以进行排序。

    7

    a.exe_plan_id = b.exe_plan_id 表示 a表的exe_plan_id字段和b表的exe_plan_id 字段进行绑定。

    可以这么理解:只有两个表绑定后才能进行相关的查询操作。

    总结通用模板:

    Select 字段名1,字段名2  case when  条件 then 执行语句  else 执行语句 end as 别名 from 表名 别名 left join on 表连接(a.exe_plan_id = b.exe_plan_id)
    Where 查询条件(d.app_time DESC)
    
    • 1
    • 2

    开发代码中如何和sql进行关联

    开发逻辑中有一个词“mapper映射”,里面具体的实现不做过多赘述。

    简单点的逻辑是:用xml文件 将sql 语句的名称 和 实体名称一一映射,通过这种形式进行绑定,然后在代码中引用映射的文件名,从而实现数据查询。

    在这里插入图片描述
    上面我们通过一段sql语句列举了MySQL常用的语法,以及sql语句编写完成后 如何在代码中进行使用的。

    现在我邀请你进入我们的软件测试学习交流群:746506216】,备注“入群”, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路。

    喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
    在这里插入图片描述

  • 相关阅读:
    Knife4j Spring Boot:在线API文档
    解决:Error: Missing binding xxxxx\node_modules\node-sass\vendor\win32-x64-83\
    Excel往Word复制表格时删除空格
    创建HTML动态数据报告;论文投稿技巧大全(中文版);『高效Pandas指南』随书代码;oneDNN深度神经网络库;前沿论文 | ShowMeAI资讯日报
    【单片机】唯一设备ID UID固件加密
    MySQL篇之SQL优化
    免费书签管理工具:浏览器书签杂乱的整理方法
    vue elementui简易侧拉栏的使用
    通过数据可观测性进行价值工程和数据成本优化
    中远麒麟堡垒机admin_commonuserSQL注入漏洞
  • 原文地址:https://blog.csdn.net/wx17343624830/article/details/125998312