gpt4 book ai didi

mysql - 匹配字段的计数(列)

转载 作者:行者123 更新时间:2023-11-29 04:01:57 24 4
gpt4 key购买 nike

出于好奇,有没有一种方法可以让我在 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

谢谢你:)

最佳答案

您需要使用 UNION ALL(not UNION only)以允许重复

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

SQLFiddle Demo

关于mysql - 匹配字段的计数(列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13083650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com