【发布时间】:2014-04-22 18:20:50
【问题描述】:
我有两个表,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 行作为输出,其中 SRCIP_TOP_TALKERS5 中不存在 5 个元组的组合
【问题讨论】:
-
伙计们,如果需要一些信息,请告诉我..