我正在使用mysql 5.5和 我有两张桌子,
+--------+---------------+---------------+
| CALLID | DL_THROUGHPUT | UL_THROUGHPUT |
+--------+---------------+---------------+
| 115 | 33.3333 | 43.3333 |
| 116 | 70 | 80 |
+--------+---------------+---------------+
+--------+----------------+----------------+
| CALLID | DL_DATA_VOLUME | UL_DATA_VOLUME |
+--------+----------------+----------------+
| 117 | 45 | 35 |
+--------+----------------+----------------+
完全加入我得到了这个:
+--------+---------------+---------------+--------+----------------+----------------+
| CALLID | DL_THROUGHPUT | UL_THROUGHPUT | CALLID | DL_DATA_VOLUME | UL_DATA_VOLUME |
+--------+---------------+---------------+--------+----------------+----------------+
| 115 | 33.3333 | 43.3333 | NULL | NULL | NULL |
| 116 | 70 | 80 | NULL | NULL | NULL |
| NULL | NULL | NULL | 117 | 45 | 35 |
+--------+---------------+---------------+--------+----------------+---------------+
我需要这个:
+--------+---------------+--------------+-----------------+----------------+
| CALLID | DL_THROUGHPUT | UL_THROUGHPUT | DL_DATA_VOLUME | UL_DATA_VOLUME |
+--------+---------------+---------------+-----------------+----------------+
| 115 | 33.3333 | 43.3333 | NULL | NULL |
| 116 | 70 | 80 | NULL | NULL |
| 117 | NULL | NULL | 45 | 35 |
+--------+---------------+---------------+-----------------+---------------+
有什么建议吗?
非常感谢你 谢谢Zane,对不起,但在我解释不好之前,我也可以将案例作为:
+--------+---------------+---------------+
| CALLID | DL_THROUGHPUT | UL_THROUGHPUT |
+--------+---------------+---------------+
| 115 | 33.3333 | 43.3333 |
| 116 | 70 | 80 |
| 117 | 48 | 51 |
+--------+---------------+---------------+
+--------+----------------+----------------+
| CALLID | DL_DATA_VOLUME | UL_DATA_VOLUME |
+--------+----------------+----------------+
| 117 | 45 | 35 |
| 118 | 37 | 26 |
+--------+----------------+----------------+
我需要:
+--------+---------------+--------------+-----------------+----------------+
| CALLID | DL_THROUGHPUT | UL_THROUGHPUT | DL_DATA_VOLUME | UL_DATA_VOLUME |
+--------+---------------+---------------+-----------------+----------------+
| 115 | 33.3333 | 43.3333 | NULL | NULL |
| 116 | 70 | 80 | NULL | NULL |
| 117 | 48 | 51 | 45 | 35 |
| 118 | NULL | NULL | 37 | 26 |
+--------+---------------+---------------+-----------------+---------------+
答案 0 :(得分:3)
您可以手动插入NULL
值,因为您知道它们不适用于最终结果:
SELECT CALLID,
DL_THROUGHPUT,
UL_THROUGHPUT,
NULL AS DL_DATA_VOLUME,
NULL AS UL_DATA_VOLUME
FROM tbl_1
UNION ALL
SELECT CALLID,
NULL,
NULL,
DL_DATA_VOLUME,
UL_DATA_VOLUME
FROM tbl_2
您可以使用:
SELECT a.CALLID,
b.DL_THROUGHPUT,
b.UL_THROUGHPUT,
c.DL_DATA_VOLUME,
c.UL_DATA_VOLUME
FROM (
SELECT CALLID FROM tbl_1 UNION SELECT CALLID FROM tbl_2
) a
LEFT JOIN tbl_1 b ON a.CALLID = b.CALLID
LEFT JOIN tbl_2 c ON a.CALLID = c.CALLID
ORDER BY a.CALLID
答案 1 :(得分:1)
尝试此查询 -
SELECT
t.CALLID,
t1.DL_THROUGHPUT,
t2.UL_THROUGHPUT,
t1.DL_DATA_VOLUME,
t2.UL_DATA_VOLUME
FROM (SELECT CALLID FROM table1 UNION SELECT CALLID FROM table2) t
LEFT JOIN table1 t1
ON t1.CALLID = t.CALLID
LEFT JOIN table2 t2
ON t2.CALLID = t.CALLID
答案 2 :(得分:0)
如果您需要加入,我会选择:
SELECT callid, dl_throughput, ul_throughput, dl_data_volume, ul_data_volume
FROM table1
FULL JOIN table2 USING(callid)