0

例如,我有两个数据表:

网络状态

ipaddress       time       violation
192.168.1.1     2:00       yes
192.168.1.6     2:00       no
192.168.1.11    2:00       no
192.168.1.1     3:00       no
192.168.1.6     3:00       no
192.168.1.11    3:00       yes

防火墙日志

ipaddress       machinetype    location
192.168.1.1     computer       London
192.168.1.6     server         New York
192.168.1.11    server         Bejing

并且我希望将防火墙日志中的数据合并到网络状态表中,因此我将拥有以下内容:

网络状态

ipaddress       time       violation       machinetype       location
192.168.1.1     2:00       yes             computer          London
192.168.1.6     2:00       no              server            New York
192.168.1.11    2:00       no              server            Bejing
192.168.1.1     3:00       no              computer          London
192.168.1.6     3:00       no              server            New York
192.168.1.11    3:00       yes             server            Bejing

有没有人有任何想法?我假设会有一个命令通过特定的列值合并表,但经过研究,我只遇到了 join 和 select into 命令。但我不知道如何使用这些来实现我想要的。

谢谢!

4

2 回答 2

3

您如何看待视图?将两个表与一个选择组合并从中创建一个视图

CREATE VIEW myView AS SELECT log.*,status.* FROM log, status 
WHERE log.ipaddress=status.ipaddress 

/* OR */
CREATE VIEW myView AS SELECT log.*,status.* FROM log l
INNER JOIN status s ON l.ipaddress=s.ipaddress
于 2012-11-21T14:49:01.633 回答
1

要“合并”此表,您需要:

1)在“网络状态”表中添加“机器类型”和“位置”列:

ALTER TABLE "Network Status"
  ADD COLUMN "machinetype" TEXT;
ALTER TABLE "Network Status"
  ADD COLUMN "location" TEXT;

2)用数据填充它们

UPDATE "Network Status" NS
SET "machinetype" = FL."machinetype",
    "location" = FL."location"
FROM "Firewall Log" FL
WHERE FL."ipaddress" = NS."ipaddress"

或者您可以创建一个VIEW并将其用于SELECT数据,就好像它是一个表一样:

CREATE VIEW "Status and log" AS
SELECT NS."ipaddress", NS."time", NS."violation", FL."machinetype", FL."location"
FROM "Network Status" NS
JOIN "Firewall Log" FL ON FL."ipaddress" = NS."ipaddress"
于 2012-11-21T15:44:43.763 回答