Reputation: 11
I have 2 queries reproducing 2 results as:
Result 1:
select title1,age1 from tab1 where title1 in ('1','2')
title1 age1
1 2
2 3
Result 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.
== update the schema of table
+--------+---------+------+-----+---------+-------+
| 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 ;
It produced 4 rows as:
+--------+------+--------+------+
| title1 | age1 | title2 | age2 |
+--------+------+--------+------+
| 1 | 2 | a | b |
| 2 | 3 | a | b |
| 1 | 2 | c | d |
| 2 | 3 | c | d |
+--------+------+--------+------+
Upvotes: 0
Views: 4783
Reputation: 3656
I think this will work good for you.
-- Example
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
Upvotes: 2
Reputation: 1837
select title_1, age_1 from table1
union
select title_2, age_2 from table2
Result is
title_1 age_1 title_2 age_2
1 2
2 3
a b
c d
Upvotes: 0
Reputation: 20806
Without seeing your SQL queries, it's hard to give you an exact answer, but you should be looking to merge them together, something like this:
SELECT title_1, age_1, title_2, age_2
FROM table1, table2
WHERE table1.id = table2.id
Upvotes: 0