日期:2014-05-16 浏览次数:20492 次
select a.* from T1 a
union all
select b.* from T2 b
order by 单号,组号
IF EXISTS( SELECT NAME FROM sys.objects AS o WHERE NAME = 't1')
DROP TABLE t1
GO
CREATE TABLE t1(orderno VARCHAR(3) , groupno VARCHAR(1) , product NVARCHAR(5) , place NVARCHAR(5))
GO
INSERT INTO t1(orderno , groupno , product , place)
SELECT '001' , '1' , N'香蕉' , N'广东' union all
SELECT '001' , '1' , N'苹果' , N'辽宁' union all
SELECT '001' , '2' , N'葡萄' , N'台湾' union all
SELECT '001' , '1' , N'西瓜' , N'太原' union all
SELECT '001' , '2' , N'柿子' , N'辽宁'
IF EXISTS(SELECT NAME FROM sys.objects AS o WHERE o.name = 't2')
DROP TABLE t2
GO
CREATE TABLE t2(orderno VARCHAR(3),groupno VARCHAR(1) , remark NVARCHAR(4))
GO
INSERT INTO t2(orderno , groupno , remark)
SELECT '001' , '1' , N'空运' union all
SELECT '001' , '2' , N'陆运'
GO
--执行查询
SELECT orderno , groupno , product , place , 1 AS t FROM t1
UNION ALL
SELECT orderno , groupno , remark AS product, '' AS place , 2 AS t FROM t2
ORDER BY orderno , groupno , t
/*执行结果
orderno groupno product place t
------- ------- ------- ----- -----------
001 1 香蕉 广东 1
001 1