我得到两张这样的表:
create table #table1 (data1 int)
create table #table2 (data2 int)
insert into #table1 (data1) values (1),(2),(3)
insert into #table2 (data2) values (4),(5),(6)
我想进行查询,返回两列,如下所示:
data1 data2
1 4
2 5
3 6
我找到了一个解决方案:
select t1.data1, t2.data2 from
(select row_number() over (order by data1) as [Index], data1 from #table1) as t1 inner join
(select row_number() over (order by data2) as [Index], data2 from #table2) as t2 on (t1.[Index] = t2.[Index])
您是否知道其他方式连接表(除了交叉之外的任何连接)没有键?
编辑:我在没有cursor
和temporary tables
的情况下寻找解决方案。
答案 0 :(得分:1)
create table table1 (data1 int)
create table table2 (data2 int)
insert into table1 (data1) values (1),(2),(3)
insert into table2 (data2) values (4),(5),(6)
create table #table1
(id int identity(1,1),
data1 int)
create table #table2
(id int identity(1,1),
data2 int)
insert into #table1 (data1) select data1 from table1
insert into #table2 (data2) select data2 from table2
create table table3
(data1 int,
data2 int)
insert into table3 (data1, data2)
select #table1.data1, #table2.data2
from #table1
inner join #table2 on #table1.id = #table2.id
答案 1 :(得分:0)
如果我插入以下值,将在这种情况下输出什么 插入#table1(data1)值(1),(2),(3) 插入#table2(data2)值(2),(3),(4) 你可能需要
select t1.data1, t2.data2 from t1,t2