【发布时间】:2017-06-28 01:58:04
【问题描述】:
我有两张桌子:
temp 有 7027 行,其架构如下所示
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| ip | varchar(32) | YES | | NULL | |
| month | int(2) | YES | | NULL | |
| day | int(2) | YES | | NULL | |
| hour | int(2) | YES | | NULL | |
| totcount | bigint(21) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
temp1 再次有 4972 行,架构如下所示
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| ip | varchar(32) | YES | | NULL | |
| month | int(2) | YES | | NULL | |
| day | int(2) | YES | | NULL | |
| hour | int(2) | YES | | NULL | |
| compcount | bigint(21) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
我想要做的是将来自 temp1 的列 compcount 附加到 temp 中的数据,在没有 compcount 值的任何地方使用 0 或空值。 temp1 中存在的所有 ip、月、日、小时组合也存在于 temp 中,但反之则不然。
举个例子,如果我的 temp 如下:
+----------------+-------+------+------+----------+
| ip | month | day | hour | totcount |
+----------------+-------+------+------+----------+
| 0.0.0.0 | 1 | 17 | 0 | 215 |
| 0.0.0.0 | 1 | 18 | 1 | 490 |
| 0.0.0.0 | 1 | 19 | 0 | 749 |
| 0.0.0.0 | 1 | 20 | 0 | 471 |
| 0.0.0.0 | 1 | 21 | 15 | 330 |
| 0.0.0.0 | 1 | 22 | 15 | 45 |
| 0.0.0.0 | 1 | 23 | 14 | 214 |
| 0.0.0.0 | 2 | 25 | 1 | 13 |
| 1.1.1.1 | 1 | 17 | 21 | 58 |
| 1.1.1.1 | 1 | 18 | 22 | 70 |
| 1.1.1.1 | 1 | 20 | 22 | 89 |
| 1.1.1.1 | 1 | 21 | 11 | 67 |
+----------------+-------+------+------+----------+
和 temp1:
+----------------+-------+------+------+----------+
| ip | month | day | hour | compcount|
+----------------+-------+------+------+----------+
| 0.0.0.0 | 1 | 17 | 0 | 100 |
| 0.0.0.0 | 1 | 18 | 1 | 176 |
| 0.0.0.0 | 1 | 21 | 15 | 182 |
| 0.0.0.0 | 1 | 22 | 15 | 36 |
| 0.0.0.0 | 1 | 23 | 14 | 198 |
| 1.1.1.1 | 1 | 17 | 21 | 46 |
| 1.1.1.1 | 1 | 18 | 22 | 53 |
| 1.1.1.1 | 1 | 20 | 22 | 27 |
| 1.1.1.1 | 1 | 21 | 11 | 61 |
+----------------+-------+------+------+----------+
那么我希望结果表是:
+----------------+-------+------+------+----------+----------+
| ip | month | day | hour | totcount |compcount |
+----------------+-------+------+------+----------+----------+
| 0.0.0.0 | 1 | 17 | 0 | 215 | 100 |
| 0.0.0.0 | 1 | 18 | 1 | 490 | 176 |
| 0.0.0.0 | 1 | 19 | 0 | 749 | 0 |
| 0.0.0.0 | 1 | 20 | 0 | 471 | 0 |
| 0.0.0.0 | 1 | 21 | 15 | 330 | 182 |
| 0.0.0.0 | 1 | 22 | 15 | 45 | 36 |
| 0.0.0.0 | 1 | 23 | 14 | 214 | 198 |
| 0.0.0.0 | 2 | 25 | 1 | 13 | 0 |
| 1.1.1.1 | 1 | 17 | 21 | 58 | 46 |
| 1.1.1.1 | 1 | 18 | 22 | 70 | 53 |
| 1.1.1.1 | 1 | 20 | 22 | 89 | 27 |
| 1.1.1.1 | 1 | 21 | 11 | 67 | 61 |
+----------------+-------+------+------+----------+----------+
我试过查询
select temp.ip, temp.month, temp.day, temp.hour, temp.totcount,
temp1.compcount from temp1 left outer join temp on
temp.ip=temp1.ip and temp.month=temp1.month and
temp.day=temp1.day and temp.hour=temp1.hour order by ip;
但它只返回 4972 行。如何构造查询以返回我要查找的内容?
【问题讨论】:
-
如果切换到“外连接”或“右连接”会发生什么?试图确认差异并非意料之外。
标签: mysql