如何找到来自另一个表MYSQL查询的序列?

时间:2019-04-10 08:20:42

标签: mysql sequence

我在mysql数据库中有2个表。第一个是结果,第二个是testrun表。结果表包含测试数据,而testrun表包含需要测试的测试。

我需要知道在testrun中可以找到的序列是否存在于结果中,并且结果在该序列中也不能为0。

**testrun**
    +---------+------------+----------+----------------+
    | BoardId | TestSpecId | sequence | ExecutionOrder |
    +---------+------------+----------+----------------+
    |    9000 |          1 |      400 |              1 |
    |    9000 |          1 |      401 |              2 |
    |    9000 |          2 |        4 |              1 |
    |    9000 |          2 |        2 |              2 |
    |    9000 |          2 |        3 |              3 |
    |    9000 |          2 |        1 |              4 |
    |    9001 |          1 |      401 |              1 |
    |    9001 |          1 |      403 |              2 |
    |    9001 |          1 |      404 |              3 |
    |    9001 |          2 |        5 |              1 |
    |    9001 |          2 |        2 |              2 |
    |    9001 |          2 |        3 |              3 |
    |    9001 |          2 |        1 |              4 |
    |    9001 |          2 |        4 |              5 |
    +---------+------------+----------+----------------+
**results**
+---------+--------+------------------+------------+----------+----------------+
| BoardId | Result | ProductionLinkId | TestSpecId | sequence | ExecutionOrder |
+---------+--------+------------------+------------+----------+----------------+
|    9000 |      1 |              200 |          1 |      400 |              1 |
|    9000 |      1 |              200 |          1 |      401 |              2 |
      Sequence Passed
|    9000 |      1 |              200 |          2 |        4 |              1 |
|    9000 |      1 |              200 |          2 |        2 |              2 |
|    9000 |      1 |              200 |          2 |        3 |              3 |
       Missing a value in sequence: sequence failed
|    9001 |      1 |              300 |          1 |      401 |              1 |
|    9001 |      1 |              300 |          1 |      403 |              2 |
|    9001 |      1 |              300 |          1 |      404 |              3 |
      Sequence passed
|    9001 |      1 |              300 |          2 |        5 |              1 |
|    9001 |      1 |              300 |          2 |        2 |              2 |
|    9001 |      1 |              300 |          2 |        3 |              3 |
|    9001 |  **0** |              300 |          2 |        1 |              4 |
|    9001 |      1 |              300 |          2 |        4 |              5 |
       Sequence contains 0: sequence failed
|    9001 |      1 |              400 |          1 |      401 |              1 |
|    9001 |      1 |              400 |          1 |      403 |              2 |
|    9001 |      1 |              400 |          1 |      404 |              3 |
      Sequence passed
|    9001 |      1 |              500 |          1 |      401 |              1 |
|    9001 |      1 |              500 |          1 |      403 |              2 |
|    9001 |      1 |              500 |          1 |      404 |              3 |
      Sequence passed
|    9001 |      1 |              500 |          2 |        5 |              1 |
|    9001 |      1 |              500 |          2 |        2 |              2 |
|    9001 |  **0** |              500 |          2 |        3 |              3 |
|    9001 |      1 |              500 |          2 |        5 |              1 |
|    9001 |      1 |              500 |          2 |        2 |              2 |
|    9001 |      1 |              500 |          2 |        3 |              3 |
|    9001 |      1 |              500 |          2 |        1 |              4 |
|    9001 |      1 |              500 |          2 |        4 |              5 |
      Sequence passed because sequence comes after each other and its all 1
+---------+--------+------------------+------------+----------+----------------+

我们需要通过sql查询获得的结果如下

+---------+------------------+------------+---------+
| BoardId | ProductionLinkId | TestSpecId | Results |
+---------+------------------+------------+---------+
|    9000 |              200 |          1 |       1 |
|    9000 |              200 |          2 |       0 |
|    9001 |              300 |          1 |       1 |
|    9001 |              300 |          2 |       0 |
|    9001 |              400 |          1 |       1 |
|    9001 |              500 |          1 |       1 |
|    9001 |              500 |          2 |       1 |
+---------+------------------+------------+---------+
    

因此,根据特定的boardId,特定的ProductionLinkId和特定的TestSpecId,需要一个结果。

SELECT * FROM testrun,results where 
testrun.BoardId = results.boardId 
and testrun.TestSpecId = results.TestSpecId 
and testrun.SequenceId = results.sequenceId 

用小提琴更新: DB fiddle

select * from (select * from results where result = 1) as res
    left outer join (SELECT * FROM testrun) as run on res.BoardId = run.boardId and res.TestSpecId = run.TestSpecId and res.SequenceId = run.sequenceId
union
select * from (select * from results where result = 1) as res
    right outer join (SELECT * FROM testrun) as run on res.BoardId = run.boardId and res.TestSpecId = run.TestSpecId and res.SequenceId = run.sequenceId 
order by ProductionLinkId

查看小提琴: DB fiddle
在这里,您可以获得缺少的值,但是现在就不再从哪个ProductionLinkId中获取值,以及是否存在运行所有测试所需的正确序列。

这是一个包含更多数据的示例文件: Example SQL file


经过长时间的搜索,我终于找到了它。

SELECT res.BoardId, res.ProductionLinkId, res.TestSpecId, res.execu, spec.exec, if(res.execu LIKE CONCAT('%',spec.exec,'%'),1,0) FROM
    (SELECT BoardId, TestSpecId, group_concat(ExecutionOrder order by ExecutionOrder) as exec FROM testrun group by BoardId, TestSpecId) as spec
inner join
    (SELECT BoardId, ProductionLinkId, TestSpecId, group_concat(ExecutionOrder order by resultId) as execu FROM (SELECT * FROM results where result = 1) as resultGrpSpec group by BoardId, ProductionLinkId, TestSpecId) as res
on spec.BoardId = res.BoardId and spec.TestSpecId = res.TestSpecId

查看小提琴: DB fiddle

0 个答案:

没有答案