使用两个条件获取计数的嵌套查询

Posted

技术标签:

【中文标题】使用两个条件获取计数的嵌套查询【英文标题】:Nested queries to get count with two conditions 【发布时间】:2012-11-07 06:52:18 【问题描述】:

我有这 3 张桌子,

tbl_1-
ip  |isp    |infection
----------------------
1   |aaaa   |malware
2   |bbbb   |malware
3   |cccc   |ddos
3   |cccc   |***
4   |dddd   |ddos

tbl_2-
ip  |isp    |infection
----------------------
1   |aaaa   |malware
3   |cccc   |ddos
4   |dddd   |***
5   |eeee   |***
6   |ffff   |other

tbl_3-
ip  |isp    |infection
----------------------
1   |aaaa   |ddos
6   |ffff   |
2   |bbbb   |other

我需要得到如下结果,

result-
ip  |isp    |infection  |ipCount    |ispCount   |infectionCount
--------------------------------------------------------------
1   |aaaa   |malware    |3          |3          |2
1   |aaaa   |ddos       |3          |3          |1
2   |bbbb   |other      |2          |2          |1
2   |bbbb   |malware    |2          |2          |1
3   |cccc   |ddos       |3          |3          |2
3   |cccc   |***     |3          |3          |1
4   |dddd   |ddos       |2          |2          |1
4   |dddd   |***     |2          |2          |1
5   |eeee   |***     |1          |1          |1
6   |ffff   |other      |2          |2          |1
6   |ffff   |           |2          |2          |1

ipCount, ispCount -> count of matching ip and isp
    eg-there are 3 records with ip = 1 and isp = aaaa

infectionCount -> count of matching infections per ip and isp
    eg-there are 2 infections that says malware where ip = 1 and isp = aaaa

我想我需要一个嵌套查询,但我不知道如何计算两个条件;你能帮忙吗?

EDIT:我试过的代码,

SELECT ip, isp, infection, count(ip), count(isp), count(infection)
FROM (

SELECT ip, isp, infection
FROM tbl_1
UNION ALL
SELECT ip, isp, infectionType
FROM tbl_2
UNION ALL
SELECT ip, isp, infection
FROM tbl_3
)x

GROUP BY ip, isp, infection

但它没有给出我想要的结果,因为我不知道如何在一个查询中进行 2 种类型的计数

【问题讨论】:

所以what have you tried? 我试过工会,我贴出代码.. ipCount 何时与 ispCount 不同? 除非isp名称不同,否则都是一样的 @hims056 我发布了我尝试过的代码 【参考方案1】:

您需要对列 infection 和 (ip & ipc) 进行不同的分组,然后使用这样的子查询加入它们:

SELECT t1.ip, t1.isp, t2.infection, t1.ipc, t1. ispc, t2.incount
FROM
    (SELECT ip, isp, infection, COUNT(ip) as ipc, COUNT(isp) as ispc
    FROM (
       SELECT ip, isp, infection
       FROM tbl1
       UNION ALL
       SELECT ip, isp, infection
       FROM tbl2
       UNION ALL
       SELECT ip, isp, infection
       FROM tbl3
       )x
     GROUP BY ip, isp) t1
JOIN
    (SELECT ip, isp, infection, COUNT(infection) as incount
     FROM (
       SELECT ip, isp, infection
       FROM tbl1
       UNION ALL
       SELECT ip, isp, infection
       FROM tbl2
       UNION ALL
       SELECT ip, isp, infection
       FROM tbl3
       )x
    GROUP BY ip, isp,  infection)t2
ON t1.ip = t2.ip
ORDER BY ip, isp, infection Desc

See this SQLFiddle

注意:我认为你想要的输出是错误的,因为:

    Table3 中,ip=6 没有 infection,但它在您的输出中 infection other 在您的输出中丢失(取而代之的是 malware

【讨论】:

heheheee..thank you :) 是的..我做了编辑..谢谢你指出 :)【参考方案2】:

您可以将所有表合并在一起,然后对列进行求和并按特定列分组。

SELECT ip, isp, infection, COUNT(ip) AS ipcount, COUNT(isp) AS ispcount, COUNT(infection) AS infectioncount
FROM
  (
    SELECT ip, isp, infection
    FROM table_1
    UNION ALL
    SELECT ip, isp, infection
    FROM table_2
    UNION ALL
    SELECT ip, isp, infection
    FROM table_3
  )
GROUP BY ip, isp, infection
ORDER BY ip, isp, infection;

【讨论】:

使用 GROUP BY 对输出进行排序就足够了。 不..这是我之前尝试过的相同解决方案..但不是这样:(感谢您的回复:)

以上是关于使用两个条件获取计数的嵌套查询的主要内容,如果未能解决你的问题,请参考以下文章

mongodb 查询求助,嵌套数组里面查东西

如何使用嵌套案例查询获得不同的计数?

用于计数的嵌套计数查询的 SQL 语法问题?

与删除嵌套查询合并,不给出删除计数

oracle 嵌套查询 子查询 自连接 等值连接条件

从 Firebase 中的嵌套查询填充回收站视图