如何并排合并两个表的结果? [英] How to join the results of 2 tables side-by-side?
本文介绍了如何并排合并两个表的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个查询,它们再现了2个结果,
I have 2 queries reproducing 2 results as:
结果1:
select title1,age1 from tab1 where title1 in ('1','2')
title1 age1
1 2
2 3
结果2:
select title2,age2 from tab2 where title2 in ('a','c')
title2 age2
a b
c d
我希望我的结果简单地并排在一起作为最终结果:
I want my results simply joined together side-by-side as a final result:
title1 age1 title2 age2
1 2 a b
2 3 c d
如何轻松实现这一目标?我搜索了但没有找到解决方案.
How do I achieve this in an easy way? I searched but didn't find the solution.
==更新表的模式
+--------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| title1 | int(11) | YES | | NULL | |
| age1 | int(11) | YES | | NULL | |
+--------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> describe tab2;
+--------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| title2 | varchar(2) | YES | | NULL | |
| age2 | varchar(2) | YES | | NULL | |
+--------+------------+------+-----+---------+-------+
===更新: 我尝试了以下操作:
=== update: I tried following:
select * from
(select title1, age1 from tab1 order by title1,age1) as result1 ,
(select title2, age2 from tab2 order by title2,age2) as result2 ;
它产生了4行:
+--------+------+--------+------+
| title1 | age1 | title2 | age2 |
+--------+------+--------+------+
| 1 | 2 | a | b |
| 2 | 3 | a | b |
| 1 | 2 | c | d |
| 2 | 3 | c | d |
+--------+------+--------+------+
推荐答案
我认为这对您有用.
-示例
select result1.title1, title1.age1,result2.title2, title2.age2 from
(select @i:=@i+1 AS rowId, title1, age1 from tab1,(SELECT @i:=0) a) as result1 ,
(select @j:=@j+1 AS rowId,title2, age2 from tab2,(SELECT @j:=0) a ) as result2
where
result1.rowId = result2.rowId;
-- try this it will work perfectly fine
这篇关于如何并排合并两个表的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文