首页 > 代码库 > union联合查询将两个表的查询结果在一个表中显示

union联合查询将两个表的查询结果在一个表中显示

表格ChannelReward和表格RewardSuppleSettle中都有一下几个字段JSMonth, ChannelNo, RewardSum, RewardItem。要将两个表中RewardSum>0的记录都显示在一个表中,可以使用联合查询

select * from(
SELECT     JSMonth, ChannelNo, RewardSum, RewardItem, ‘1‘ AS ObjectType
FROM         dbo.ChannelReward
WHERE     (RewardSum > 0)
UNION ALL
SELECT     JSMonth, ChannelNo, Reward AS RewardSum, ItemName AS RewardItem, ‘1‘ AS ObjectType
FROM         RewardSuppleSettle
WHERE     (Reward > 0))t where t.ChannelNo=‘1003‘