001 内容a1
001 内容a2
001 内容a3
表B的记录是 ID Content
001 内容b1
001 内容b2
怎么把他们连接成
ID aContent bContent
001 内容a1 内容b1
001 内容a2 内容b1
001 内容a3 null
注意还有其他的记录,我只是列举了卡号为001的记录,如能解决不胜感激!!
7 个解决方案
#1
select
a.id,
a.Content as aContent,
b.Content as bContent
from
A
left join
B
on
a.id=b.id
#2
修正一下
---测试数据---
if object_id('[A]') is not null drop table [A]
go
create table [A]([ID] varchar(3),[Content] varchar(6))
insert [A]
select '001','内容a1' union all
select '001','内容a2' union all
select '001','内容a3'
if object_id('[B]') is not null drop table [B]
go
create table [B]([ID] varchar(3),[Content] varchar(6))
insert [B]
select '001','内容b1' union all
select '001','内容b2'
---查询---
select
a.id,
a.Content as aContent,
b.Content as bContent
from
(select *,px=(select count(1) as cnt from A t where t.id=a.id and t.content<a.content) from A) as A
left join
(select *,px=(select count(1) as cnt from B t where t.id=b.id and t.content<b.content) from B) as B
on
a.px=b.px
---结果---
id aContent bContent
---- -------- --------
001 内容a1 内容b1
001 内容a2 内容b2
001 内容a3 NULL
(所影响的行数为 3 行)
#3
如果存在B表比A表多的记录也要显示出来,则用full join
#4
DECLARE @TA TABLE([ID] VARCHAR(3), [Content] NVARCHAR(4))
INSERT @TA
SELECT '001', N'内容a1' UNION ALL
SELECT '001', N'内容a2' UNION ALL
SELECT '001', N'内容a3'
DECLARE @TB TABLE([ID] VARCHAR(3), [Content] NVARCHAR(4))
INSERT @TB
SELECT '001', N'内容b1' UNION ALL
SELECT '001', N'内容b2'
SELECT A.ID,A.Content,B.Content
FROM (
SELECT *,SEQ=ROW_NUMBER() OVER (ORDER BY GETDATE()) FROM @TA)A FULL JOIN (
SELECT *,SEQ=ROW_NUMBER() OVER (ORDER BY GETDATE()) FROM @TB) B ON A.SEQ=B.SEQ
/*
ID Content Content
---- ------- -------
001 内容a1 内容b1
001 内容a2 内容b2
001 内容a3 NULL
*/
#5
同意 Josy 的接法
#6
不过,修正一下取后的 ID:
select
IsNull(a.id,b.id) as id,
a.Content as aContent,
b.Content as bContent
from
A FULL join B
on
a.id=b.id
select
IsNull(a.id,b.id) as id,
a.Content as aContent,
b.Content as bContent
from
A FULL join B
on
a.id=b.id
#7
最后再加个Order by IsNull(a.ID,B.ID),A.Content,B.Content就是你想要的结果了
#1
select
a.id,
a.Content as aContent,
b.Content as bContent
from
A
left join
B
on
a.id=b.id
#2
修正一下
---测试数据---
if object_id('[A]') is not null drop table [A]
go
create table [A]([ID] varchar(3),[Content] varchar(6))
insert [A]
select '001','内容a1' union all
select '001','内容a2' union all
select '001','内容a3'
if object_id('[B]') is not null drop table [B]
go
create table [B]([ID] varchar(3),[Content] varchar(6))
insert [B]
select '001','内容b1' union all
select '001','内容b2'
---查询---
select
a.id,
a.Content as aContent,
b.Content as bContent
from
(select *,px=(select count(1) as cnt from A t where t.id=a.id and t.content<a.content) from A) as A
left join
(select *,px=(select count(1) as cnt from B t where t.id=b.id and t.content<b.content) from B) as B
on
a.px=b.px
---结果---
id aContent bContent
---- -------- --------
001 内容a1 内容b1
001 内容a2 内容b2
001 内容a3 NULL
(所影响的行数为 3 行)
#3
如果存在B表比A表多的记录也要显示出来,则用full join
#4
DECLARE @TA TABLE([ID] VARCHAR(3), [Content] NVARCHAR(4))
INSERT @TA
SELECT '001', N'内容a1' UNION ALL
SELECT '001', N'内容a2' UNION ALL
SELECT '001', N'内容a3'
DECLARE @TB TABLE([ID] VARCHAR(3), [Content] NVARCHAR(4))
INSERT @TB
SELECT '001', N'内容b1' UNION ALL
SELECT '001', N'内容b2'
SELECT A.ID,A.Content,B.Content
FROM (
SELECT *,SEQ=ROW_NUMBER() OVER (ORDER BY GETDATE()) FROM @TA)A FULL JOIN (
SELECT *,SEQ=ROW_NUMBER() OVER (ORDER BY GETDATE()) FROM @TB) B ON A.SEQ=B.SEQ
/*
ID Content Content
---- ------- -------
001 内容a1 内容b1
001 内容a2 内容b2
001 内容a3 NULL
*/
#5
同意 Josy 的接法
#6
不过,修正一下取后的 ID:
select
IsNull(a.id,b.id) as id,
a.Content as aContent,
b.Content as bContent
from
A FULL join B
on
a.id=b.id
select
IsNull(a.id,b.id) as id,
a.Content as aContent,
b.Content as bContent
from
A FULL join B
on
a.id=b.id
#7
最后再加个Order by IsNull(a.ID,B.ID),A.Content,B.Content就是你想要的结果了