zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    错误需要理由吗?(SQL Server 不存在或访问被拒绝)
    关于权限设计的轻量级实现
    ppc通过数据线与共享电脑上网
    解读.Net中的命名空间和程序集
    .Net XML 树
    软件开发教父与国内高手论道实录全文
    企业信息化, 该怎么规划?
    ASP.NET Portal Starter Kit中的角色验证
    用js实现类似分享到显示效果
    用js实现同一个页面多个渐变效果
  • 原文地址:https://www.cnblogs.com/lqsilly/p/2989552.html
Copyright © 2011-2022 走看看