Joining multiple selects based on a parameter
I have two queries which return the following results (A) & (B) say:
SELECT username, ext_num FROM user u
JOIN extension e
ON u.id=e.user_id;
+----------+---------+
| username | ext_num |
+----------+---------+
| test | 2459871 |
+----------+---------+
1 row in set (0.00 sec)
SELECT TIME_TO_SEC(TIMEDIFF(od.created_at, oc.created_at)) as `duration
(sec)`, oc.ext_num, oc.destination, oc.created_at, oc.call_id
-> FROM on_connected oc
-> JOIN on_disconnected od ON od.call_id = oc.call_id
-> WHERE oc.ext_num = 2459871\G;
*************************** 1. row ***************************
duration (sec): 4
ext_num: 2459871
destination: 55544466677
created_at: 2013-08-19 17:11:53
call_id: 521243ad953e-965inwuz1gku
*************************** 2. row ***************************
duration (sec): 4
ext_num: 2459871
destination: 55544466677
created_at: 2013-08-20 10:28:48
call_id: 521336b51225-0w4mkelwpfui
2 rows in set (0.00 sec)
I would like to join the two tables above to return something like:
+----------------+----------------+-----------------------+---------------------+---------------------------+
| username | duration (sec) | ext_num | destination | created_at
| call_id |
+----------------+----------------+-----------------------+---------------------+---------------------------+
| test | 4 | 2459871 | 55544466677 | 2013-08-19
17:11:53 | 521243ad953e-965inwuz1gku |
| test | 4 | 2459871 | 55544466677 | 2013-08-20
10:28:48 | 521336b51225-0w4mkelwpfui |
+----------------+----------------+-----------------------+---------------------+---------------------------+
I could then, in theory, return all phone calls made for any particular
'ext_num' say or finer grained reporting on 'call_id' if needed.
What have I tried? Well I initially thought of the UNION operator:
(A) UNION (B);
where (A) was padded with NULL values in the SELECT statement but this
produced unstable results.
+----------------+---------+-------------+---------------------+
| duration (sec) | ext_num | destination | created_at |
+----------------+---------+-------------+---------------------+
| 4 | 2459871 | 55544466677 | 2013-08-19 17:11:53 |
| 4 | 2459871 | 55544466677 | 2013-08-20 10:28:48 |
| test | 2459871 | NULL | NULL |
+----------------+---------+-------------+---------------------+
3 rows in set (0.01 sec)
No comments:
Post a Comment