sqlite 2表与关系表

时间:2017-03-25 04:29:05

标签: sqlite

我正在努力提取RedHat漏洞信息而且我被困在sql查询中:)基本上我有2个表(cve,勘误表),其中保存数据,第三个(引用)是带有ID的参考表从表errata和cve。

我想要提取的是来自两个表的信息,如下所示:

name_from_errata   name_from_cve  severity_from_errata   releasedata_from_errata
RHSA-2017:0837     CVE-2017-5208    important     2017-03-23...
RHSA-2017:0837     CVE-2017-5332    moderate      2017-03-23...
RHSA-2017:0837     CVE-2017-5333    important     ............
RHSA-2017:0837     CVE-2017-6009    important     ............
.........................

谢谢!

马里奥

例如,此链接列出了6个漏洞 RedHat Advisory sample

CVE-2017-5208
CVE-2017-5332
CVE-2017-5333
CVE-2017-6009
CVE-2017-6010
CVE-2017-6011

Sqlite3数据库@ cvrf

[localhost cvrf]$ sqlite3 cvrf.db
sqlite> .headers on
sqlite> .mode column
sqlite> .tables
cve           errata        package       relationship`

sqlite> .schema cve
CREATE TABLE "cve" ("id" INTEGER NOT NULL PRIMARY KEY, "name" VARCHAR(255) NOT NULL, "severity" VARCHAR(255), "cvss_score" VARCHAR(255), "publicdate" DATETIME);
CREATE UNIQUE INDEX "cve_name" ON "cve" ("name");`

sqlite> .schema errata
CREATE TABLE "errata" ("id" INTEGER NOT NULL PRIMARY KEY, "name" VARCHAR(255) NOT NULL, "severity" VARCHAR(255), "releasedate" DATETIME);
CREATE UNIQUE INDEX "errata_name" ON "errata" ("name");`

sqlite> .schema relationship
CREATE TABLE "relationship" ("id" INTEGER NOT NULL PRIMARY KEY, "errata_id" INTEGER NOT NULL, "cve_id" INTEGER NOT NULL, FOREIGN KEY ("errata_id") REFERENCES "errata" ("id"), FOREIGN KEY ("cve_id") REFERENCES "cve" ("id"));
CREATE INDEX "relationship_errata_id" ON "relationship" ("errata_id");
CREATE INDEX "relationship_cve_id" ON "relationship" ("cve_id");
CREATE UNIQUE INDEX "relationship_errata_id_cve_id" ON "relationship" ("errata_id", "cve_id");

获取过去2天的建议清单(我需要最后一天,但今天没有建议)

sqlite> SELECT * FROM errata WHERE releasedate > (SELECT DATETIME('now', '-2 day'));
id          name            severity    releasedate        
----------  --------------  ----------  --------------------
2722        RHSA-2017:0837  important   2017-03-23T00:21:00Z
2723        RHSA-2017:0838  moderate    2017-03-23T02:35:00Z
2724        RHSA-2017:0484  moderate    2017-03-23T04:59:00Z
2725        RHSA-2017:0494  moderate    2017-03-23T04:59:00Z
2726        RHSA-2017:0495  moderate    2017-03-23T05:04:00Z
2727        RHSA-2017:0486  moderate    2017-03-23T05:04:00Z

获取与勘误表关联的ID列表是' 2722'

sqlite> select cve_id from relationship where errata_id='2722';
cve_id   
----------
5002     
5003     
5004     
5005     
5006     
5007

获取与勘误名称RHSA-2017相关的cve名称列表:0837(勘误表编号5002)

qlite> select * from cve where id='5002';
id          name           severity    cvss_score  publicdate         
----------  -------------  ----------  ----------  --------------------
5002        CVE-2017-5208  important   NULL        2017-01-08T00:00:00Z

sqlite> select * from cve where id='5003';
id          name           severity    cvss_score  publicdate         
----------  -------------  ----------  ----------  --------------------
5003        CVE-2017-5332  moderate    NULL        2017-01-10T00:00:00Z

sqlite> select * from cve where id='5004';
id          name           severity    cvss_score  publicdate         
----------  -------------  ----------  ----------  --------------------
5004        CVE-2017-5333  important   NULL        2017-01-10T00:00:00Z

sqlite> select * from cve where id='5005';
id          name           severity    cvss_score  publicdate         
----------  -------------  ----------  ----------  --------------------
5005        CVE-2017-6009  important   NULL        2017-02-16T00:00:00Z

sqlite> select * from cve where id='5006';
id          name           severity    cvss_score  publicdate         
----------  -------------  ----------  ----------  --------------------
5006        CVE-2017-6010  moderate    NULL        2017-02-16T00:00:00Z

sqlite> select * from cve where id='5007';
id          name           severity    cvss_score  publicdate         
----------  -------------  ----------  ----------  --------------------
5007        CVE-2017-6011  moderate    NULL        2017-02-16T00:00:00Z

1 个答案:

答案 0 :(得分:0)

加入表格:

SELECT e.name, c.name, e.severity, e.releasedate
FROM errata e
JOIN relationship r ON e.id = r.errata_id
JOIN cve c ON c.id = r.cve_id
WHERE e.releasedate > DATETIME('now', '-2 day')