Mysql 如何并排连接两个表的结果?

Mysql 如何并排连接两个表的结果?,mysql,sql,join,Mysql,Sql,Join,我有两个查询,其中两个结果如下: 结果1: select title1,age1 from tab1 where title1 in ('1','2') 结果2: select title2,age2 from tab2 where title2 in ('a','c') 我希望我的结果只是并排连接在一起作为最终结果: title1 age1 title2 age2 1 2 a b 2 3 c d 如何以简单的方式实现

我有两个查询,其中两个结果如下:

结果1:

select title1,age1 from tab1 where title1 in ('1','2')

结果2:

select title2,age2 from tab2 where title2 in ('a','c')

我希望我的结果只是并排连接在一起作为最终结果:

title1 age1 title2 age2
1       2     a        b
2       3     c        d
如何以简单的方式实现这一点?我搜索了一下,但没有找到答案

==更新表的架构

+--------+---------+------+-----+---------+-------+
| 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    |       |
+--------+------------+------+-----+---------+-------+
==更新: 我尝试了以下几点:

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    |
+--------+------+--------+------+

在没有看到SQL查询的情况下,很难给出准确的答案,但您应该将它们合并在一起,如下所示:

SELECT  title_1, age_1, title_2, age_2
FROM    table1, table2
WHERE   table1.id = table2.id
结果是

title_1 age_1 title_2 age_2
1       2     
2       3    
              a        b
              c        d

我认为这对你有好处

--范例


欢迎来到堆栈溢出!请将问题包含在查询代码中。发布表架构。如果有的话,关系列是什么?子查询中的Order by WITH limit没有任何意义,任何顺序都是一致的。添加了查询命令。你的方式是先加入,然后选择。我要先选择,然后加入。已尝试此操作。它产生了与“交叉连接”相同的结果,其末尾有4行。我只想要2行。已尝试所有查询。全部生成4行而不是2行。您可以使用where子句添加条件以删除重复的列。我已编辑了答案,这将非常适合您,而无需连接键列。只是为了添加,Oracle中提供了rowID和rowNum,但MYSQL中没有提供,所以我使用了一个变通方法来执行相同的操作
SELECT  title_1, age_1, title_2, age_2
FROM    table1, table2
WHERE   table1.id = table2.id
select title_1, age_1 from table1
union
select title_2, age_2 from table2
title_1 age_1 title_2 age_2
1       2     
2       3    
              a        b
              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