• SQL UNION 和 UNION ALL 操作符


      UNION 操作符用于合并两个或多个 SELECT 语句的结果集。请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。SQL UNION 语法,SELECT column_name(s) FROM table_name1
      UNION
    SELECT column_name(s) FROM table_name2
    注释:默认地,UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL。
      SQL UNION ALL 语法
    SELECT column_name(s) FROM table_name1 UNION ALL SELECT column_name(s) FROM table_name2另外,UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。
    下面演示一个UNION实例和实例操作后显示数据
    select max(dr.ScriptName) as 分类汇总,
    sum(dr.ExecuteTrimLenght) as 业务函数总用时,
    sum(dr.ExecuteTrimLenght) as 业务函数成功总用时,
    0 as 业务函数失败总用时,
    
    dr.IsSucceed as 成功与否
                                            from ScriptExecuteDetailResult as dr left join  ScriptExecuteResut as er on (dr.ser_id = er.id) left join
                                            TaskExecuteResult as ter on (er.ter_id = ter.id) left join 
                                            TaskOrders as eor on (ter.taskOrders_id = eor.id) left join ExecuteResult as er on (dr.ID = er.CaseId)
                                            where dr.isDel = 0  and dr.IsSucceed <> 'ON' 
                                            group by dr.ScriptName
    
    union 
    select max(dr.ScriptName) as 分类汇总,
    sum(dr.ExecuteTrimLenght) as 业务函数总用时,
    sum(dr.ExecuteTrimLenght) as 业务函数成功总用时,
    0 as 业务函数失败总用时,
    
    dr.IsSucceed as 成功与否
                                            from ScriptExecuteDetailResult as dr left join  ScriptExecuteResut as er on (dr.ser_id = er.id) left join
                                            TaskExecuteResult as ter on (er.ter_id = ter.id) left join 
                                            TaskOrders as eor on (ter.taskOrders_id = eor.id) left join ExecuteResult as er on (dr.ID = er.CaseId)
                                            where dr.isDel = 0  and dr.IsSucceed <> 'ON' 
    and IsSucceed = '成功'
                                            group by dr.ScriptName
    
    union 
    select max(dr.ScriptName) as 分类汇总,
    sum(dr.ExecuteTrimLenght) as 业务函数总用时,
    0 as 业务函数总用时成功,
    sum(dr.ExecuteTrimLenght) as 业务函数失败总用时,
    
    dr.IsSucceed  as 成功与否
                                            from ScriptExecuteDetailResult as dr left join  ScriptExecuteResut as er on (dr.ser_id = er.id) left join
                                            TaskExecuteResult as ter on (er.ter_id = ter.id) left join 
                                            TaskOrders as eor on (ter.taskOrders_id = eor.id) left join ExecuteResult as er on (dr.ID = er.CaseId)
                                            where dr.isDel = 0  and dr.IsSucceed <> 'ON' 
    and IsSucceed = '失败'
                                            group by dr.ScriptName

  • 相关阅读:
    二进制安装postgresql
    php mongodb扩展安装
    coinbase交易冻结时间
    linux服务器及工具常用命令
    PM2下使用 执行npm命令
    phpstorm安装PHP_CodeSniffer
    TP5 save遍历更新,过滤相邻重复字段,问题汇总
    mysqlbinlog 数据恢复
    设计模式之状态机模式
    关于c++中sleep_for函数的总结分析
  • 原文地址:https://www.cnblogs.com/lqsilly/p/2989552.html
Copyright © 2020-2023  润新知