表A
品种(豆类)
黄豆
绿豆
土豆
豌豆
白云豆表B
品种(肉类)
猪肉
牛肉
鸡肉上两个表中各有一个列,现在要将两个表合成一个有两个列的表,两个列之间没有关系,只是顺序显示便于查看。如下:品种(豆类) 品种(肉类)
黄豆 猪肉
绿豆 牛肉
土豆 鸡肉
豌豆
白云豆
品种(豆类)
黄豆
绿豆
土豆
豌豆
白云豆表B
品种(肉类)
猪肉
牛肉
鸡肉上两个表中各有一个列,现在要将两个表合成一个有两个列的表,两个列之间没有关系,只是顺序显示便于查看。如下:品种(豆类) 品种(肉类)
黄豆 猪肉
绿豆 牛肉
土豆 鸡肉
豌豆
白云豆
(select t.* , px = (select count(1) from a where 品种 < t.品种) + 1 from a t) m
full join
(select t.* , px = (select count(1) from a where 品种 < t.品种) + 1 from b t) n
on m.px = n.px
insert into a values(N'黄豆')
insert into a values(N'绿豆')
insert into a values(N'土豆')
insert into a values(N'豌豆')
insert into a values(N'白云豆') create table B([品种(肉类)] nvarchar(10))
insert into b values(N'猪肉')
insert into b values(N'牛肉')
insert into b values(N'鸡肉') select isnull(m.[品种(豆类)],'') [品种(豆类)] , isnull(n.[品种(肉类)],'') [品种(肉类)] from
(select t.* , px = (select count(1) from a where [品种(豆类)] < t.[品种(豆类)]) + 1 from a t) m
full join
(select t.* , px = (select count(1) from b where [品种(肉类)] < t.[品种(肉类)]) + 1 from b t) n
on m.px = n.px
order by m.pxdrop table A , B/*
品种(豆类) 品种(肉类)
---------- ----------
白云豆 鸡肉
黄豆 牛肉
绿豆 猪肉
土豆
豌豆 (所影响的行数为 5 行)*/
from
(select t.* , px = (select count(1) from a where 品种 < t.品种) + 1 from a t) m
full join
(select t.* , px = (select count(1) from a where 品种 < t.品种) + 1 from b t) n
on m.px = n.px
add id int identity
go
alter table tb
add id int identity
select isnull(a.品种,'') 品种A , isnull(b.品种,'') 品种B from ta a left join tb b on
a.id=b.id
go
alter table ta
drop column id
go
alter table tb
drop column id
insert into a values(N'黄豆')
insert into a values(N'绿豆')
insert into a values(N'土豆')
insert into a values(N'豌豆')
insert into a values(N'白云豆') create table B([品种(肉类)] nvarchar(10))
insert into b values(N'猪肉')
insert into b values(N'牛肉')
insert into b values(N'鸡肉')
go--1。如果要按照你的品种顺序排,需要使用临时表.
select * , px = identity(int,1,1) into temp_a from a
select * , px = identity(int,1,1) into temp_b from b
select isnull(m.[品种(豆类)],'') [品种(豆类)] , isnull(n.[品种(肉类)],'') [品种(肉类)]
from temp_a m full join temp_b n
on m.px = n.px
order by m.px
/*
品种(豆类) 品种(肉类)
---------- ----------
黄豆 猪肉
绿豆 牛肉
土豆 鸡肉
豌豆
白云豆 (所影响的行数为 5 行)
*/--2。如果不需要按照顺序,不需要临时表。使用自我连接先排序。
select isnull(m.[品种(豆类)],'') [品种(豆类)] , isnull(n.[品种(肉类)],'') [品种(肉类)] from
(select t.* , px = (select count(1) from a where [品种(豆类)] < t.[品种(豆类)]) + 1 from a t) m
full join
(select t.* , px = (select count(1) from b where [品种(肉类)] < t.[品种(肉类)]) + 1 from b t) n
on m.px = n.px
order by m.px
/*
品种(豆类) 品种(肉类)
---------- ----------
白云豆 鸡肉
黄豆 牛肉
绿豆 猪肉
土豆
豌豆 (所影响的行数为 5 行)*/
drop table A , B , temp_a , temp_b
go
create table tA(品种 nvarchar(10))
insert into ta values(N'黄豆')
insert into ta values(N'绿豆')
insert into ta values(N'土豆')
insert into ta values(N'豌豆')
insert into ta values(N'白云豆')
if object_id('tb')is not null drop table tb
go
create table tB(品种 nvarchar(10))
insert into tb values(N'猪肉')
insert into tb values(N'牛肉')
insert into tb values(N'鸡肉')
go
alter table ta
add id int identity
go
alter table tb
add id int identity
go
select isnull(a.品种,'') 品种A , isnull(b.品种,'') 品种B from ta a left join tb b on
a.id=b.id
go
alter table ta
drop column id
go
alter table tb
drop column id
/*品种A 品种B
---------- ----------
黄豆 猪肉
绿豆 牛肉
土豆 鸡肉
豌豆
白云豆 */
create table A([品种(豆类)] nvarchar(10))
insert into a values(N'黄豆')
insert into a values(N'绿豆')
insert into a values(N'土豆')
insert into a values(N'豌豆')
insert into a values(N'白云豆') create table B([品种(肉类)] nvarchar(10))
insert into b values(N'猪肉')
insert into b values(N'牛肉')
insert into b values(N'鸡肉')
go--1。如果要按照你的品种顺序排,需要使用临时表.
select * , px = identity(int,1,1) into temp_a from a
select * , px = identity(int,1,1) into temp_b from b
select isnull(m.[品种(豆类)],'') [品种(豆类)] , isnull(n.[品种(肉类)],'') [品种(肉类)]
from temp_a m full join temp_b n
on m.px = n.px
order by m.px
/*
品种(豆类) 品种(肉类)
---------- ----------
黄豆 猪肉
绿豆 牛肉
土豆 鸡肉
豌豆
白云豆 (所影响的行数为 5 行)
*/--2。如果不需要按照顺序,不需要临时表。使用自我连接先排序。
select isnull(m.[品种(豆类)],'') [品种(豆类)] , isnull(n.[品种(肉类)],'') [品种(肉类)] from
(select t.* , px = (select count(1) from a where [品种(豆类)] < t.[品种(豆类)]) + 1 from a t) m
full join
(select t.* , px = (select count(1) from b where [品种(肉类)] < t.[品种(肉类)]) + 1 from b t) n
on m.px = n.px
order by m.px
/*
品种(豆类) 品种(肉类)
---------- ----------
白云豆 鸡肉
黄豆 牛肉
绿豆 猪肉
土豆
豌豆 (所影响的行数为 5 行)*/
drop table A , B , temp_a , temp_b
--sql server 2005
create table A([品种(豆类)] nvarchar(10))
insert into a values(N'黄豆')
insert into a values(N'绿豆')
insert into a values(N'土豆')
insert into a values(N'豌豆')
insert into a values(N'白云豆') create table B([品种(肉类)] nvarchar(10))
insert into b values(N'猪肉')
insert into b values(N'牛肉')
insert into b values(N'鸡肉')
go
--1。如果要按照你的品种顺序排,需要使用临时表.
select * , px = identity(int,1,1) into temp_a from a
select * , px = identity(int,1,1) into temp_b from b
select isnull(m.[品种(豆类)],'') [品种(豆类)] , isnull(n.[品种(肉类)],'') [品种(肉类)]
from temp_a m full join temp_b n
on m.px = n.px
order by m.px
/*
品种(豆类) 品种(肉类)
---------- ----------
黄豆 猪肉
绿豆 牛肉
土豆 鸡肉
豌豆
白云豆 (5 行受影响)
*/--2。如果不需要按照顺序,不需要临时表。使用row_number排序。
select isnull(m.[品种(豆类)],'') [品种(豆类)] , isnull(n.[品种(肉类)],'') [品种(肉类)] from
(select a.* , px = row_number() over(order by [品种(豆类)]) from a) m
full join
(select b.* , px = row_number() over(order by [品种(肉类)]) from b) n
on m.px = n.px
order by m.px
/*品种(豆类) 品种(肉类)
---------- ----------
土豆 牛肉
白云豆 猪肉
绿豆 鸡肉
豌豆
黄豆 (5 行受影响)
*/drop table A , B , temp_a , temp_b
if object_id('[A]') is not null drop table [A]
go
create table [A]([品种] varchar(6))
insert [A]
select '黄豆' union all
select '绿豆' union all
select '土豆' union all
select '豌豆' union all
select '白云豆'
if object_id('[B]') is not null drop table [B]
go
create table [B]([品种] varchar(4))
insert [B]
select '猪肉' union all
select '牛肉' union all
select '鸡肉'
---查询---
select AID=IDENTITY(INT,1,1),品种 as [品种(豆类)] INTO #A FROM A
select BID=IDENTITY(INT,1,1),品种 as [品种(肉类)] INTO #B FROM BSELECT #a.[品种(豆类)],ISNULL(#b.[品种(肉类)],'') AS [品种(肉类)]
FROM #A
LEFT JOIN #B ON #A.AID=#B.BIDDROP TABLE #A
DROP TABLE #B---结果---
品种(豆类) 品种(肉类)
------ ------
黄豆 猪肉
绿豆 牛肉
土豆 鸡肉
豌豆
白云豆 (所影响的行数为 5 行)
create table A([品种(豆类)] nvarchar(10))
insert into a values(N'黄豆')
insert into a values(N'绿豆')
insert into a values(N'土豆')
insert into a values(N'豌豆')
insert into a values(N'白云豆') create table B([品种(肉类)] nvarchar(10))
insert into b values(N'猪肉')
insert into b values(N'牛肉')
insert into b values(N'鸡肉') ;with x as
(
select *,ord=(row_number() over (order by [品种(豆类)])) from A
),
y as
(
select *,ord=(row_number() over (order by [品种(肉类)])) from B
)
select x.[品种(豆类)],y.[品种(肉类)] from x
full join y on x.ord=y.ord
/*
品种(豆类) 品种(肉类)
白云豆 鸡肉
黄豆 牛肉
绿豆 猪肉
土豆 NULL
豌豆 NULL
*/