我有两个查询返回以下结果(A)和(B)说:
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)
我想加入上面的两个表以返回如下内容:
+----------------+----------------+-----------------------+---------------------+---------------------------+
| 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 |
+----------------+----------------+-----------------------+---------------------+---------------------------+
然后,理论上,我可以返回为任何特定的“ext_num”打的所有电话,或者如果需要的话,可以更精细地报告“call_id”。
我尝试了什么?好吧,我最初想到了 UNION 运算符:
(A) UNION (B);
其中 (A) 在 SELECT 语句中填充了 NULL 值,但这会产生不稳定的结果。
+----------------+---------+-------------+---------------------+
| 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)