日期:2014-05-19  浏览次数:20511 次

求一个简单的SQL语句
select   A   from   table1   where   条件1
查询结果为
1
2
3
select   B   from   table2   where   条件2
查询结果为




有没有什么办法把两个结果拼接在一起
使得查询结果为
1甲
2乙
3丙

------解决方案--------------------
select identity(int,1,1) as id,A into #t1
from table1 where 条件1

select identity(int,1,1) as id,B into #t2
from table2 where 条件2

select a.f1,b.f1 from
#t1 a,#t2 b
where a.id=b.id
------解决方案--------------------
2个table必须要有主外键关系...
连接查询...
------解决方案--------------------
这种问题需要问一句:
为什么是1对应甲,2对应乙,有什么规律吗?
------解决方案--------------------
create table A(col int)
insert A select 1
insert A select 2
insert A select 3
create table B(col varchar(10))
insert B select '甲 '
union all select '乙 '
union all select '丙 '

select ID=identity(int, 1, 1), * into #A from A
select ID=identity(int, 1, 1), * into #B from B

select A.col, B.col
from #A A
left join #B B on A.ID=B.ID

--result
col col
----------- ----------
1 甲
2 乙
3 丙

(3 row(s) affected)
------解决方案--------------------
如果是SQL2005可以不用,2000或以下版本需要生成递增列