sql 合并两个查询结果

来源:百度知道 编辑:UC知道 时间:2024/05/22 20:07:04
查询结果1
1 a
2 b
3 c
查询结果2
1 d
2 e
3 f
想要的结果
1 a d
2 b e
3 c f
这两个查询结果是通过多表查询得出的,其中有group by和order by子句。并不是简单的两张表。
而且我没有创建表的权限。
我只有查询的权限。

用临时表。
查询结果1:
select ...
into #temp1
from ...
where ...
group by ...
order by ...
查询结果2:
select ...
into #temp2
from ...
where ...
group by ...
order by ...
合并:
select #temp1.a,#temp2.b,#temp2.c
from #temp1 inner join #temp2 on #temp1.id=#temp2.id

select t1.数字字段名,t1.abc字段名+t2.def字段名
from t1 ,t2
where t1.数字字段名 = t2.数字字段名

可以创建临时表 在select啊
create table #表名
(。。。。。

使用内连接查询可以实现;查询结果为横向显示;
select * from 表1 as 别名1 inner join 表2 as 别人2 on(别名1.id=别名2.id)

或者用联合查询;

select * from 表1
unioe
select * from 表2 . 要求表1和表2的字段个数相同.查询结果为纵向显示

select t1.数字字段名,t1.abc字段名,t2.def字段名 from t1
inner join t2
on t1.数字字段名 = t2.数字字段名