MySQL查询结合了几个表

时间:2014-10-23 15:53:41

标签: python mysql database

背景

为了获得我论文的数据,我必须公平地工作 复杂的MySQL数据库,包含几个表和数百GB 数据。不幸的是,我是SQL的新手,并且无法真正弄清楚如何做到 提取我需要的数据。

数据库

数据库由我想要组合的几个表组成。这里有 相关部分:

> show tables;
+---------------------------+
| Tables_in_database        |
+---------------------------+
| Build                     |
| Build_has_ModuleRevisions |
| Configuration             |
| ModuleRevisions           |
| Modules                   |
| Product                   |
| TestCase                  |
| TestCaseResult            |
+---------------------------+

表格按以下方式链接在一起

Product ---(1:n)--> Configurations ---(1:n)--> Build

Build ---(1:n)--> Build_has_ModuleRevisions ---(n:1)--> ModuleRevision ---(n:1)--> Modules

Build ---(1:n)--> TestCaseResult ---(n:1)--> TestCase

表的内容是

> describe Product;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(255) | NO   | UNI | NULL    |                |
+---------+--------------+------+-----+---------+----------------+


> describe Configuration;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| Product_id | int(11)      | YES  | MUL | NULL    |                |
| name       | varchar(255) | NO   | UNI | NULL    |                |
+------------+--------------+------+-----+---------+----------------+


> describe Build;
+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| id               | int(11)      | NO   | PRI | NULL    | auto_increment |
| Configuration_id | int(11)      | NO   | MUL | NULL    |                |
| build_number     | int(11)      | NO   | MUL | NULL    |                |
| build_id         | varchar(32)  | NO   | MUL | NULL    |                |
| test_status      | varchar(255) | NO   |     |         |                |
| start_time       | datetime     | YES  | MUL | NULL    |                |
| end_time         | datetime     | YES  | MUL | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+


> describe Build_has_ModuleRevisions;
+-------------------+----------+------+-----+---------+----------------+
| Field             | Type     | Null | Key | Default | Extra          |
+-------------------+----------+------+-----+---------+----------------+
| id                | int(11)  | NO   | PRI | NULL    | auto_increment |
| Build_id          | int(11)  | NO   | MUL | NULL    |                |
| ModuleRevision_id | int(11)  | NO   | MUL | NULL    |                |
+-------------------+----------+------+-----+---------+----------------+


> describe ModuleRevisions;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int(11)      | NO   | PRI | NULL    | auto_increment |
| Module_id | int(11)      | NO   | MUL | NULL    |                |
| tag       | varchar(255) | NO   | MUL |         |                |
| revision  | varchar(255) | NO   | MUL |         |                |
+-----------+--------------+------+-----+---------+----------------+


> describe Modules;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(255) | NO   | UNI | NULL    |                |
+---------+--------------+------+-----+---------+----------------+


> describe TestCase;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| TestSuite_id | int(11)      | NO   | MUL | NULL    |                |
| classname    | varchar(255) | NO   | MUL | NULL    |                |
| name         | varchar(255) | NO   | MUL | NULL    |                |
| testtype     | varchar(255) | NO   | MUL | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+


> describe TestCaseResult;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| Build_id    | int(11)      | NO   | MUL | NULL    |                |
| TestCase_id | int(11)      | NO   | MUL | NULL    |                |
| status      | varchar(255) | NO   | MUL | NULL    |                |
| start_time  | datetime     | YES  | MUL | NULL    |                |
| end_time    | datetime     | YES  | MUL | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

如您所见,表格与*_id字段相关联。例如。 TestCaseResultBuild链接到Build_id field,而TestCase连接到TestCase_id Configuration.name字段。

问题解决

现在我的问题。给定具体的Product.nameBuild 输入,我需要找到所有模块+修订和失败的测试用例 Build.start_time,按Build排序。

我尝试过什么

以下查询为我提供了Configuration.name的所有config1 Product.nameproduct1 SELECT * FROM `database`.`Build` AS b JOIN Configuration AS c ON c.id = b.Configuration_id JOIN Product as p ON p.id = c.Product_id WHERE c.name = 'config1' AND p.name = 'product1' ORDER BY b.start_time;

Modules

但这并不能解决我问题的一半。现在,对于每个构建我 需要

  1. 查找与Build关联的所有 Modules.name
    • 提取ModuleRevision.revision字段
    • 提取TestCase字段
  2. 查找与Build关联的所有 TestCaseResult.status = 'failure'
    • TestCase.name
    • 提取与TestCaseResult
    • 相关联的Build字段
  3. Build.start_time与提取的模块名称+修订版和测试用例相关联 名称
  4. 显示由Modules.name排序的数据,以便我可以执行 分析它。
  5. 换句话说,在所有可用数据中,我只对连接数据感兴趣 字段ModuleRevision.revisionTestCaseResult.statusTestCaseResult.nameBuild Build.start_time到特定Build Build.start_time Modules+Revisions Failed tests 1 20140301 [(mod1, rev1), (mod2... etc] [test1, test2, ...] 2 20140401 [(mod1, rev2), (mod2... etc] [test1, test2, ...] 3 20140402 [(mod3, rev1), (mod2... etc] [test1, test2, ...] 4 20140403 [(mod1, rev3), (mod2... etc] [test1, test2, ...] 5 20140505 [(mod5, rev2), (mod2... etc] [test1, test2, ...] ,请按{{1}}订购 然后将其输出到我编写的Python程序中。

    最终结果应与

    类似
    {{1}}

    我的问题

    是否有一个可以提取的优秀(且优选高效)的SQL查询 提供我需要的数据?

    如果没有,我完全可以提取一个或几个超集/子集 数据,以便在必要时使用Python解析它。但是我如何提取 想要的数据?

1 个答案:

答案 0 :(得分:1)

在我看来,你需要不止一个查询。问题是Build <-> ModuleRevisionBuild <- TestCaseResult的关系基本上是独立的。就模式而言,ModuleRevisionTestCaseResult并没有真正相互关联。你必须查询一个然后查询另一个。您不能在一个查询中同时获取它们,因为结果中的每一行基本上代表“最深”相关表(在本例中为ModuleRevisionTestCaseResult)的一条记录,包括任何相关信息从其父表。因此,我认为您需要以下内容:

SELECT
    M.name, MR.revision, B.id
FROM
    ModuleRevisions MR
INNER JOIN
    Modules M ON MR.Module_id = M.id
INNER JOIN
    Build_has_ModuleRevisions BHMR ON MR.id = BHMR.ModuleRevision_id
INNER JOIN
    Build B ON BHMR.Build_id = B.id
INNER JOIN
    Configuration C ON B.Configuration_id = C.id
INNER JOIN
    Product P ON C.Product_id = P.id
WHERE C.name = 'config1' AND P.name = 'product1'
ORDER BY B.start_time;

SELECT
    TCR.status, TC.name, B.id
FROM
    TestCaseResult TCR
INNER JOIN
    TestCase TC ON TCR.TestCase_id = TC.id
INNER JOIN
    Build B ON TCR.Build_id = B.id
INNER JOIN
    Configuration C ON B.Configuration_id = C.id
INNER JOIN
    Product P ON C.Product_id = P.id
WHERE C.name = 'config1' AND P.name = 'product1' and TCR.status = 'failure'
ORDER BY B.start_time;