我有2个查询,将2个结果复制为:
结果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
我希望我的结果可以简单地并排连接在一起作为最终结果:
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 |
+--------+------+--------+------+
答案 0 :(得分:1)
我认为这对你有用。
- 示例
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
答案 1 :(得分:0)
如果没有看到您的SQL查询,很难给出确切的答案,但您应该将它们合并在一起,如下所示:
SELECT title_1, age_1, title_2, age_2
FROM table1, table2
WHERE table1.id = table2.id
答案 2 :(得分:0)
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