从一个表中选择数据,该表在Mysql的另一个表中不存在

时间:2014-04-21 18:50:43

标签: mysql join

我有两个表,TABLE1和TABLE2。

TABLE2包含表中的记录,其中包含少量列。每次调用该过程时,它都会检查是否存在列组合(复合键,但未声明)。

如果存在,它会更新其中一个列,否则会生成一个新条目。

desc MAIN_TBL;
+---------------+-------------+------+-----+---------------------+----------------+
| Field         | Type        | Null | Key | Default             | Extra          |
+---------------+-------------+------+-----+---------------------+----------------+
| id            | int(11)     | NO   | PRI | NULL                | auto_increment |
| LinkID        | int(11)     | YES  | MUL | NULL                |                |
| Protocol      | varchar(10) | YES  |     | NULL                |                |
| SourceIP      | varchar(30) | YES  |     | NULL                |                |
| DestinationIP | varchar(30) | YES  |     | NULL                |                |
| SourcePort    | int(11)     | YES  |     | NULL                |                |
| DestPort      | int(11)     | YES  |     | NULL                |                |
| NoOfBytes     | int(11)     | YES  |     | NULL                |                |
| insertTime    | timestamp   | NO   |     | CURRENT_TIMESTAMP   |                |
| StartTime     | timestamp   | NO   |     | 0000-00-00 00:00:00 |                |
| EndTime       | timestamp   | NO   |     | 0000-00-00 00:00:00 |                |
| Direction     | varchar(10) | YES  |     | NULL                |                |
| Trafficbps    | int(11)     | YES  |     | NULL                |                |
| nFlows        | int(11)     | YES  |     | NULL                |                |
| flag          | tinyint(1)  | YES  |     | 0                   |                |
+---------------+-------------+------+-----+---------------------+----------------+

数据

select Protocol,SourceIP,DestinationIP,SourcePort,DestPort,NoOfBytes from MAIN_TBL;
+----------+----------+---------------+------------+----------+-----------+
| Protocol | SourceIP | DestinationIP | SourcePort | DestPort | NoOfBytes |
+----------+----------+---------------+------------+----------+-----------+
| TCP      | 1.1.1.1  | 2.2.2.2       |       1080 |      443 |     10000 |
| TCP      | 1.1.1.1  | 2.2.2.2       |       1080 |     8080 |     20000 |
| TCP      | 1.1.1.1  | 2.2.2.2       |       1090 |     8080 |     20000 |
+----------+----------+---------------+------------+----------+-----------+

表2:

desc SRCIP_TOP_TALKERS5;
+---------------+-------------+------+-----+-------------------+-------+
| Field         | Type        | Null | Key | Default           | Extra |
+---------------+-------------+------+-----+-------------------+-------+
| SourceIP      | varchar(30) | YES  |     | NULL              |       |
| DestinationIP | varchar(30) | YES  |     | NULL              |       |
| DestPort      | int(11)     | YES  |     | NULL              |       |
| SourcePort    | int(11)     | YES  |     | NULL              |       |
| Protocol      | varchar(10) | YES  |     | NULL              |       |
| NoOfBytes     | int(11)     | YES  |     | NULL              |       |
| Time_1        | timestamp   | NO   |     | CURRENT_TIMESTAMP |       |
+---------------+-------------+------+-----+-------------------+-------+

数据

select Protocol,SourceIP,DestinationIP,SourcePort,DestPort,NoOfBytes from SRCIP_TOP_TALKERS5;
+----------+----------+---------------+------------+----------+-----------+
| Protocol | SourceIP | DestinationIP | SourcePort | DestPort | NoOfBytes |
+----------+----------+---------------+------------+----------+-----------+
| TCP      | 1.1.1.1  | 2.2.2.2       |       1080 |      443 |     10000 |
+----------+----------+---------------+------------+----------+-----------+

现在,我的程序首先更新 SRCIP_TOP_TALKERS5 中组合(SourcePort,DestPort,SourceIP,DestinationIP,Protocol)的条目。

查询是:

update SRCIP_TOP_TALKERS5 T1 INNER JOIN MAIN_TBL T2
                        on
                        T1.SourceIP = T2.SourceIP and
                        T1.DestinationIP = T2.DestinationIP and
                        T1.DestPort = T2.DestPort and
                        T1.SourcePort = T2.SourcePort and
                        T1.Protocol = T2.Protocol
                        set T1.NoOfBytes=T1.NoOfBytes+T2.NoOfBytes;

在此之后,我希望插入剩余的行,找不到匹配项。 如果您考虑我现有的数据(如上所示),我应该从MAIN_TBL获得2行输出,其中5个元组的组合在SRCIP_TOP_TALKERS5中不存在

2 个答案:

答案 0 :(得分:0)

你可以做一个正确的加入。

UPDATE SRCIP_TOP_TALKERS5 T1
RIGHT JOIN MAIN_TBL T2 ON T1.SourceIP = T2.SourceIP
AND T1.DestinationIP = T2.DestinationIP
AND T1.DestPort = T2.DestPort
AND T1.SourcePort = T2.SourcePort
AND T1.Protocol = T2.Protocol
set T1.NoOfBytes= COALESCE(T1.NoOfBytes,0) + T2.NoOfBytes;

如果T1不是T2的子集,请执行T2的COALESCE。让我知道,如果这适合您。

答案 1 :(得分:0)

我通过引用SO上的其他链接找到答案。

我需要做一个左连接并进行空检查(这里重要的是从select中的两个表中拉出所有列)

select T1.NoOfBytes as NoOfBytes,T1.Protocol as Protocol,T1.SourcePort as SourcePort,T1.SourceIP as SourceIP,
                    T1.DestinationIP as DestinationIP,T1.DestPort as DestPort,
                    T2.Protocol as Proto,T2.SourcePort as srcPrt,T2.SourceIP as SrcIP,T2.DestinationIP as DestIP,T2.DestPort as DstPrt
                    from MAIN_TBL T1 left join SRCIP_TOP_TALKERS5 T2
                    on T1.SourceIP = T2.SourceIP and
                            T1.DestinationIP = T2.DestinationIP and
                            T1.DestPort = T2.DestPort and
                            T1.SourcePort = T2.SourcePort and
                            T1.Protocol = T2.Protocol
                    where T2.SourcePort is null