SQL 占位符 和 union all
Posted Hello
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL 占位符 和 union all相关的知识,希望对你有一定的参考价值。
Union:用于合并查询结果集(必多个查询结果集必须有相同的列,而且有相似的类型)
比如:如果第一个查询结果为
第二个查询结果
第二个查询结果集也必须是1列,而且都是number类型(数字类型)的
Union all 区别Union
Union 相同数字合并,
查询结果集1和2合并后为
Union all不会合并结果集
实战场景(占位符和Union all):
--------------药品处方明细2-------------------------------------- select Max(PTMax) as 普通处方金额最大值, Min(PTMin) as 普通处方金额最小值, Avg(PTAvg) as 普通处方张数平均值, Max(JSYMax) as 精神一处方金额最大值, Min(JSYMin) as 精神一处方金额最小值, Avg(JSYAvg) as 精神一处方张数平均值, Max(JSEMax) as 精神二处方金额最大值, Min(JSEMin) as 精神二处方金额最小值, Avg(JSEAvg) as 精神二处方张数平均值, Max(MZMax) as 麻醉处方金额最大值, Min(MZMin) as 麻醉处方金额最小值, Avg(MZAvg) as 麻醉处方张数平均值, Max(DXMax) as 毒性处方金额最大值, Min(DXMin) as 毒性处方金额最小值, Avg(DXAvg) as 毒性处方张数平均值 from ( select Max(t.TC_OD_Sum) as PTMax , Min(t.TC_OD_Sum) as PTMin , Avg(t.TC_OD_Sum) as PTAvg , 0 as JSYMax, 0 as JSYMin, 0 as JSYAvg, 0 as JSEMax, 0 as JSEMin, 0 as JSEAvg, 0 as MZMax, 0 as MZMin, 0 as MZAvg, 0 as DXMax, 0 as DXMin, 0 as DXAvg from TB_OD_PRESCRIPTION t where exists ( select op.TC_RX_NO from TB_OD_PRESCRIPTION op join tb_dic_drug g on op.TC_OD_DG_ID=g.tc_dg_id left join Tb_Dic_Drug_Character ddc on g.tc_Ci_Id=ddc.tc_Ci_Id where ddc.Tc_Dc_Toxicologic=\'普通药\' and T.TC_RX_NO=op.TC_RX_NO ) union all select null, null, null, Max(t.TC_OD_Sum) as JSYMax , Min(t.TC_OD_Sum) as JSYMin , Avg(t.TC_OD_Sum) as JSYAvg , null, null, null, null, null, null, null, null, null from TB_OD_PRESCRIPTION t where exists ( select op.TC_RX_NO from TB_OD_PRESCRIPTION op join tb_dic_drug g on op.TC_OD_DG_ID=g.tc_dg_id left join Tb_Dic_Drug_Character ddc on g.tc_Ci_Id=ddc.tc_Ci_Id where ddc.Tc_Dc_Toxicologic=\'精神I类\' and T.TC_RX_NO=op.TC_RX_NO ) union all select null, null, null, null, null, null, Max(t.TC_OD_Sum) as JSEMax , Min(t.TC_OD_Sum) as JSEMin , Avg(t.TC_OD_Sum) as JSEAvg , null, null, null, null, null, null from TB_OD_PRESCRIPTION t where exists ( select op.TC_RX_NO from TB_OD_PRESCRIPTION op join tb_dic_drug g on op.TC_OD_DG_ID=g.tc_dg_id left join Tb_Dic_Drug_Character ddc on g.tc_Ci_Id=ddc.tc_Ci_Id where ddc.Tc_Dc_Toxicologic=\'精神II类\' and T.TC_RX_NO=op.TC_RX_NO ) union all select null, null, null, null, null, null, null, null, null, Max(t.TC_OD_Sum) as MZMax, Min(t.TC_OD_Sum) as MZMin, Avg(t.TC_OD_Sum) as MZAvg, null, null, null from TB_OD_PRESCRIPTION t where exists ( select op.TC_RX_NO from TB_OD_PRESCRIPTION op join tb_dic_drug g on op.TC_OD_DG_ID=g.tc_dg_id left join Tb_Dic_Drug_Character ddc on g.tc_Ci_Id=ddc.tc_Ci_Id where ddc.Tc_Dc_Toxicologic=\'麻醉药\' and T.TC_RX_NO=op.TC_RX_NO ) union all select null, null, null, null, null, null, null, null, null, null, null, null, Max(t.TC_OD_Sum) as DXMax, Min(t.TC_OD_Sum) as DXMin, Avg(t.TC_OD_Sum) as DXAvg from TB_OD_PRESCRIPTION t where exists ( select op.TC_RX_NO from TB_OD_PRESCRIPTION op join tb_dic_drug g on op.TC_OD_DG_ID=g.tc_dg_id left join Tb_Dic_Drug_Character ddc on g.tc_Ci_Id=ddc.tc_Ci_Id where ddc.Tc_Dc_Toxicologic=\'毒性药\' and T.TC_RX_NO=op.TC_RX_NO ) )
以上是关于SQL 占位符 和 union all的主要内容,如果未能解决你的问题,请参考以下文章