0

我正在使用 mysql 5.5,我有 2 个表,如下

+--------+---------------+---------------+
| 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 |
+--------+---------------+---------------+-----------------+---------------+

有什么建议么?

非常感谢你,谢谢赞恩,对不起,但在我解释得不好之前,我也可以这样说:

+--------+---------------+---------------+
| 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 |        
+--------+---------------+---------------+-----------------+---------------+
4

3 回答 3

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

SQLFiddle 演示

于 2012-08-01T06:51:55.970 回答
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
于 2012-08-01T07:24:13.503 回答
0

如果您需要加入,我会选择:

SELECT callid, dl_throughput, ul_throughput, dl_data_volume, ul_data_volume
  FROM table1
  FULL JOIN table2 USING(callid)
于 2012-08-01T07:26:55.543 回答