关于无关联字段外联到其他表的问题。

Dhammacakkhu 2009-04-14 01:20:19
设表1有如下字段:
ID, name
3 bill
4 paul

表2有如下字段
PlaceID, Place
1 USA
2 U.K
3 CHN


怎么实现如下效果:
PlaceID, Place ID Name
1 USA 3 bill
2 U.K 4 paul
3 CHN null null


就是说两个表没有关联字段。
...全文
93 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2009-04-14
  • 打赏
  • 举报
回复
-->SQL2000的方法(借助辅助字段方法)

alter table 表1 add px int identity --增加辅助字段

select a.*,b.[IDCardNo],b.[name] --开始查询
from 表2 a
left join 表1 b
on a.[PlaceID]=b.px

alter table 表1 drop column px --删除辅助字段
/*
PlaceID Place IDCardNo name
----------- ---------- ---------- ----------
1 USA xxxabc bill
2 U.K xxxabc paul
3 CHN NULL NULL

(3 行受影响)
Dhammacakkhu 2009-04-14
  • 打赏
  • 举报
回复
谢谢您们。结贴。
htl258_Tony 2009-04-14
  • 打赏
  • 举报
回复
if object_id('表2') is not null
drop table 表2
go
create table 表2([PlaceID] int,[Place] varchar(10))
insert 表2 select '1','USA'
insert 表2 select '2','U.K'
insert 表2 select '3','CHN'
go
if object_id('表1') is not null
drop table 表1
go
create table 表1([IDCardNo] varchar(10),[name] varchar(10))
insert 表1 select 'xxxabc','bill'
insert 表1 select 'xxxabc','paul'
go
-->SQL2005的方法
select a.*,b.[IDCardNo],b.[name]
from 表2 a
left join (select *,px=row_number() over(order by getdate()) from 表1) b
on a.[PlaceID]=b.px
/*
PlaceID Place IDCardNo name
----------- ---------- ---------- ----------
1 USA xxxabc bill
2 U.K xxxabc paul
3 CHN NULL NULL

(3 行受影响)
*/
-->SQL2000的方法(借助临时表)
select px=identity(int,1,1),* into # from 表1
select a.*,b.[IDCardNo],b.[name]
from 表2 a
left join # b
on a.[PlaceID]=b.px
drop table #
/*
PlaceID Place IDCardNo name
----------- ---------- ---------- ----------
1 USA xxxabc bill
2 U.K xxxabc paul
3 CHN NULL NULL

(3 行受影响)
*/
Dhammacakkhu 2009-04-14
  • 打赏
  • 举报
回复
2000
htl258_Tony 2009-04-14
  • 打赏
  • 举报
回复
SQL2000还是SQL2005?
Dhammacakkhu 2009-04-14
  • 打赏
  • 举报
回复
谢谢您的回答。
但如果这个表的ID是没有数字的呢,例如都是字符串,改怎么办
设表1有如下字段
IDCardNo, name
xxxabc bill
xxxabc paul


真是不好意思,然您费心了。

htl258_Tony 2009-04-14
  • 打赏
  • 举报
回复
if object_id('表2') is not null
drop table 表2
go
create table 表2([PlaceID] int,[Place] varchar(10))
insert 表2 select '1','USA'
insert 表2 select '2','U.K'
insert 表2 select '3','CHN'
go
if object_id('表1') is not null
drop table 表1
go
create table 表1([ID] varchar(10),[name] varchar(10))
insert 表1 select 'xxx001','bill'
insert 表1 select 'xxx002','paul'
go
select a.*,b.* from 表2 a left join 表1 b on a.placeID=right(b.id,3)*1

/*
PlaceID Place ID name
----------- ---------- ---------- ----------
1 USA xxx001 bill
2 U.K xxx002 paul
3 CHN NULL NULL

(3 行受影响)
*/
这样可以吗?
Dhammacakkhu 2009-04-14
  • 打赏
  • 举报
回复
但如果是这样呢:

设表1有如下字段
IDCardNo, name
xxx001 bill
xxx002 paul

表2有如下字段
PlaceID, Place
1 USA
2 U.K
3 CHN


怎么实现如下效果:
PlaceID, Place IDCardNo Name
1 USA xxx001 bill
2 U.K xxx002 paul
3 CHN null null
htl258_Tony 2009-04-14
  • 打赏
  • 举报
回复
那样符合吗?
htl258_Tony 2009-04-14
  • 打赏
  • 举报
回复
if object_id('表2') is not null
drop table 表2
go
create table 表2([PlaceID] int,[Place] varchar(10))
insert 表2 select '1','USA'
insert 表2 select '2','U.K'
insert 表2 select '3','CHN'
go
if object_id('表1') is not null
drop table 表1
go
create table 表1([ID] int,[name] varchar(10))
insert 表1 select '3','bill'
insert 表1 select '4','paul'
go
select a.*,b.* from 表2 a left join 表1 b on a.placeID=b.id-2

/*
PlaceID Place ID name
----------- ---------- ----------- ----------
1 USA 3 bill
2 U.K 4 paul
3 CHN NULL NULL

(3 行受影响)
*/
htl258_Tony 2009-04-14
  • 打赏
  • 举报
回复
select a.*,b.* from 表2 a left join 表1 b on a.placeID=b.id-2

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧