首页 > 解决方案 > MYSQL查询各种版本的常见bug

问题描述

我有不同版本的一些错误数据。我需要找出从旧版本传播的常见错误。在下面的数据示例中,我有兴趣了解此示例中的常见版本错误:“这是一个 8.x 旧错误”,即数据中的最后 3 行

9.1.1.5   |xymodel1 |227501| This is a 9.x new bug 
8.1.2.9   |xymodel1 |227501| This a 8.x new bug 
8.1.1.5   |xymodel1 |227501| This a 8.x new1 bug 
7.1.2.30  |xy6700   |287640| This is a 7.x new bug
7.1.2.30  |xy6700   |288185| This is a 7.x new1 bug
9.1.1.5   |xymodel1 |227501| This is a 9.x new bug 
10.1.2.30 |xy6700   |288368| This is a 10.x new bug 
10.1.1.6  |xymodel1 |227501| This is a 10.x new bug 
8.1.1.5   |xymodel1 |227501| This is a 8.x old bug
9.1.1.5   |xymodel1 |227501| This is a 8.x old bug
10.1.1.5  |xymodel1 |227501| This is a 8.x old bug

我运行了以下查询,它应该给了我跨 8.x、9.x 和 10.x 的常见错误,因为存在 AND 条件。但在我看到的结果中,也很少有其他伴随的行,这在三个版本中并不常见。

select * from issue_table
where
bug_name in (select bug_name from issue_table where version like '8.%' and '9.%' and '10.%')

结果:前两行不应该出现在结果中,因为它们在不同版本中并不常见。

version   model     data    bug_name
8.1.2.9   xymodel1  227501  This a 8.x new bug
8.1.1.5   xymodel1  227501  This a 8.x new1 bug
8.1.1.5   xymodel1  227501  This is a 8.x old bug
9.1.1.5   xymodel1  227501  This is a 8.x old bug
10.1.1.5  xymodel1  227501  This is a 8.x old bug

我期待:

version   model     data    bug_name
8.1.1.5   xymodel1  227501  This is a 8.x old bug
9.1.1.5   xymodel1  227501  This is a 8.x old bug
10.1.1.5  xymodel1  227501  This is a 8.x old bug

SQL Fiddle 链接可以在这里找到:https ://www.db-fiddle.com/#&togetherjs=VTGbSRMgIO

你们能帮我编辑查询以选择所有三个版本中的常见错误吗?

标签: mysqlsqlselectgroup-by

解决方案


这可能是您正在寻找的:

select * 
from issue_table
where bug_name in (
    select bug_name 
    from issue_table 
    where version like '8.%' 
    or version like '9.%' 
    or version like '10.%' 
    group by bug_name 
    having count(*) = 3)

子选择计算所有三个版本中存在的错误。

你也可以把它写成一个连接:

select it.* 
from (
    select bug_name 
    from issue_table 
    where version like '8.%' 
    or version like '9.%' 
    or version like '10.%' 
    group by bug_name 
    having count(*) = 3
) tmp, issue_table it 
where it.bug_name = tmp.bug_name

推荐阅读