出于好奇,有没有办法可以获得2个或更多表中匹配数据的数量..如果我们得到以下3个表,
tbl_1-
ip |isp |infection
----------------------
1 |aaaa |malware
2 |bbbb |malware
3 |cccc |ddos
3 |cccc |trojan
4 |dddd |ddos
tbl_2-
ip |isp |infection
----------------------
1 |aaaa |malware
3 |cccc |ddos
4 |dddd |trojan
5 |eeee |trojan
6 |ffff |other
tbl_3-
ip |isp |infection
----------------------
1 |aaaa |ddos
6 |ffff |
2 |bbbb |other
我能否得到如下所示的答案?
Result-
ip |isp |infection |Match Count
------------------------------------
1 |aaaa |malware |2
1 |aaaa |ddos |1
2 |bbbb |malware |1
2 |bbbb |other |1
3 |cccc |ddos |2
3 |cccc |trojan |1
4 |dddd |ddos |1
4 |dddd |trojan |1
5 |eeee |trojan |1
6 |ffff |other |1
6 |ffff | |1
谢谢:)
答案 0 :(得分:5)
您需要使用UNION ALL
( 不仅仅是UNION )才能允许重复
SELECT ip, isp, infection, COUNT(*)
FROM
(
SELECT ip, isp, infection FROM tbl_1
UNION ALL
SELECT ip, isp, infection FROM tbl_2
UNION ALL
SELECT ip, isp, infection FROM tbl_3
) x
GROUP BY ip, isp, infection
答案 1 :(得分:2)
您应该执行所有三个表中的UNION ALL,然后通过ip,isp,感染列运行GROUP BY。
SELECT ip, isp, infection, COUNT(*) 'Match Count'
FROM
(
SELECT * FROM tbl_1
UNION ALL
SELECT * FROM tbl_2
UNION ALL
SELECT * FROM tbl_3
) AS A
GROUP BY ip, isp, infection
答案 2 :(得分:0)