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实例和实例操作后显示数据
下面演示一个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