SQL的不同表合并,字段不同,求助!

weiomi 2014-03-02 10:14:28
数据表1
如table1
tcid 公司名称 性别 备注
1 a n 123
3 b f 256
数据表2
talbe2
listid 联系人姓名 单位 状态 性别
5 v f 145 n
1 d h 178 f
要合并成数据表3 这个样子要咋弄呢?
tcid 公司名称 性别 备注 listid 联系人姓名 单位 状态
1 a n 123 5 v f 145
3 b f 256 1 d h 178
...全文
164 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
發糞塗牆 2014-03-04
  • 打赏
  • 举报
回复
引用 6 楼 weiomi 的回复:
[quote=引用 5 楼 yupeigu 的回复:] [quote=引用 3 楼 weiomi 的回复:] [quote=引用 1 楼 yupeigu 的回复:] 是2000,还是2005呢
2000 的[/quote] 试试这个:
if OBJECT_ID('#tempdb..#table1') is not null
   drop table #table1

if OBJECT_ID('#tempdb..#table2') is not null
   drop table #table2

select IDENTITY(int,1,1) id,* into #table1
from table1

select IDENTITY(int,1,1) id,* into #table2
from table2

select tcid,公司名称,a.性别,备注,listid,联系人姓名,单位,状态 
from #table1 a
inner join #table2 b
 on a.id = b.id
  
[/quote] 啊,对啊。我怎么就没有想到用这个! 要怎么给分呢?[/quote]我被无视了?
LongRui888 2014-03-03
  • 打赏
  • 举报
回复
引用 6 楼 weiomi 的回复:
[quote=引用 5 楼 yupeigu 的回复:] [quote=引用 3 楼 weiomi 的回复:] [quote=引用 1 楼 yupeigu 的回复:] 是2000,还是2005呢
2000 的[/quote] 试试这个:
if OBJECT_ID('#tempdb..#table1') is not null
   drop table #table1

if OBJECT_ID('#tempdb..#table2') is not null
   drop table #table2

select IDENTITY(int,1,1) id,* into #table1
from table1

select IDENTITY(int,1,1) id,* into #table2
from table2

select tcid,公司名称,a.性别,备注,listid,联系人姓名,单位,状态 
from #table1 a
inner join #table2 b
 on a.id = b.id
  
[/quote] 啊,对啊。我怎么就没有想到用这个! 要怎么给分呢?[/quote] 这样结贴,然后给分:
LongRui888 2014-03-03
  • 打赏
  • 举报
回复
引用 3 楼 weiomi 的回复:
[quote=引用 1 楼 yupeigu 的回复:] 是2000,还是2005呢
2000 的[/quote] 试试这个:
if OBJECT_ID('#tempdb..#table1') is not null
   drop table #table1

if OBJECT_ID('#tempdb..#table2') is not null
   drop table #table2

select IDENTITY(int,1,1) id,* into #table1
from table1

select IDENTITY(int,1,1) id,* into #table2
from table2

select tcid,公司名称,a.性别,备注,listid,联系人姓名,单位,状态 
from #table1 a
inner join #table2 b
 on a.id = b.id
  
weiomi 2014-03-03
  • 打赏
  • 举报
回复
引用 5 楼 yupeigu 的回复:
[quote=引用 3 楼 weiomi 的回复:] [quote=引用 1 楼 yupeigu 的回复:] 是2000,还是2005呢
2000 的[/quote] 试试这个:
if OBJECT_ID('#tempdb..#table1') is not null
   drop table #table1

if OBJECT_ID('#tempdb..#table2') is not null
   drop table #table2

select IDENTITY(int,1,1) id,* into #table1
from table1

select IDENTITY(int,1,1) id,* into #table2
from table2

select tcid,公司名称,a.性别,备注,listid,联系人姓名,单位,状态 
from #table1 a
inner join #table2 b
 on a.id = b.id
  
[/quote] 啊,对啊。我怎么就没有想到用这个! 要怎么给分呢?
發糞塗牆 2014-03-03
  • 打赏
  • 举报
回复
从测试数据来看,直接用【性别】字段相连即可,这个不管2000还是2005的
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2014-03-03 07:43:59
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
--	Dec 28 2012 20:23:12 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[table1]
if object_id('[table1]') is not null drop table [table1]
go 
create table [table1]([tcid] int,[公司名称] varchar(1),[性别] varchar(1),[备注] int)
insert [table1]
select 1,'a','n',123 union all
select 3,'b','f',256
--> 测试数据:[talbe2]
if object_id('[talbe2]') is not null drop table [talbe2]
go 
create table [talbe2]([listid] int,[联系人姓名] varchar(1),[单位] varchar(1),[状态] int,[性别] varchar(1))
insert [talbe2]
select 5,'v','f',145,'n' union all
select 1,'d','h',178,'f'
--------------开始查询--------------------------


SELECT  a.* ,
        b.listid ,
        b.联系人姓名 ,
        b.单位 ,
        b.状态
FROM    [table1] a
        INNER JOIN [talbe2] b ON a.性别 = b.性别
----------------结果----------------------------
/* 
tcid        公司名称 性别   备注          listid      联系人姓名 单位   状态
----------- ---- ---- ----------- ----------- ----- ---- -----------
1           a    n    123         5           v     f    145
3           b    f    256         1           d     h    178

*/
weiomi 2014-03-02
  • 打赏
  • 举报
回复
引用 1 楼 yupeigu 的回复:
是2000,还是2005呢
2000 的
LongRui888 2014-03-02
  • 打赏
  • 举报
回复
适合2005:

select tcid,公司名称,a.性别,备注,listid,联系人姓名,单位,状态 
from 
(
select *,ROW_NUMBER() over(order by getdate()) rownum
from table1
)a
inner join 
(
select *,ROW_NUMBER() over(order by getdate()) rownum
from table2
)b
 on a.rownum = b.rownum 
LongRui888 2014-03-02
  • 打赏
  • 举报
回复
是2000,还是2005呢

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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