您需要对列进行分组infection
and (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
请参阅此 SQLFiddle http://sqlfiddle.com/#!2/56190/5
Note:我认为您想要的输出是错误的,因为:
- In
Table3
没有infection
for ip=6
但它在你的输出中
-
infection
other
您的输出中缺少(而是有malware
)