[英]Joining two tables on multiple different columns with different numbers of rows
我有两个表:
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或null值。 temp1中存在的所有ip,month,day,hour组合也都存在于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行。 如何构造查询,以使其返回我要查找的内容?
当您使用LEFT JOIN
,第一个表应该是具有所有行的表,而第二个表是可能没有匹配项的表。
因此,更改表的顺序,或使用RIGHT JOIN
。
select temp.ip, temp.month, temp.day, temp.hour, temp.totcount,
temp1.compcount
from temp
left outer join temp1 on
temp.ip=temp1.ip and temp.month=temp1.month and
temp.day=temp1.day and temp.hour=temp1.hour
order by ip, month, day, hour;
并且如果您希望结果中的0
而不是NULL
,请使用IFNULL(temp1.compcount, 0) AS compcount
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.