在具有不同行数的多个不同列上连接两个表

时间:2017-06-27 17:29:37

标签: mysql

我有两张桌子:

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行。如何构建查询以便返回我正在寻找的内容?

1 个答案:

答案 0 :(得分:1)

使用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;

DEMO

如果您希望结果中显示0而不是NULL,请使用IFNULL(temp1.compcount, 0) AS compcount