gpt4 book ai didi

mysql连接后结果不同

转载 作者:行者123 更新时间:2023-11-29 13:45:23 25 4
gpt4 key购买 nike

我对以下查询感到非常头疼。我认为问题在于 AND 部分返回的结果重复。 Mysql 对于下面的查询返回 14 个结果,但实际上只是 2 个不同的结果。

select date_format(published,'%Y-%m'),severity,count(severity) 
FROM nvdcve
LEFT JOIN nvdproducts USING(cve_id)
where (published >= '2013-06-01')
AND (
(nvdproducts.company='linux' and nvdproducts.product='linux_kernel
AND nvdproducts.version IN (SELECT Kernel from Versions.VIEW_kernel))
OR (nvdproducts.company='apache' and nvdproducts.product='http_server'
AND nvdproducts.version IN (SELECT Httpd from Versions.VIEW_httpd))
OR (nvdproducts.company='sendmail' and nvdproducts.product='sendmail' AND nvdproducts.version IN (SELECT Sendmail from Versions.VIEW_sendmail))
OR (nvdproducts.company='mysql' and nvdproducts.product='mysql' AND nvdproducts.version IN (SELECT Mysqld from Versions.VIEW_mysqld))
OR (nvdproducts.company='proftpd' and nvdproducts.product='proftpd' AND nvdproducts.version IN (SELECT Proftpd from Versions.VIEW_proftpd))
OR (nvdproducts.company='perl' and nvdproducts.product='perl' AND nvdproducts.version IN (SELECT Perl from Versions.VIEW_perl))
OR (nvdproducts.company='openssl' and nvdproducts.product='openssl' AND nvdproducts.version IN (SELECT Sslinuse from Versions.VIEW_sslinuse))
)
group by date_format(published,'%Y-%m'),severity;

此摘要查询给出此结果。

+--------------------------------+----------+-----------------+
| date_format(published,'%Y-%m') | severity | count(severity) |
+--------------------------------+----------+-----------------+
| 2013-06 | MEDIUM | 14 |
+--------------------------------+----------+-----------------+

我能得到的最接近的是获取行,但我丢失了计数并添加了 cve_id,这不是我想要的。

    select distinct cve_id,date_format(published,'%Y-%m'),severity 
FROM nvdnew.nvdcve
LEFT JOIN nvdproducts USING(cve_id)
where (published > '2013-05-31')
and published < '2013-07-01'
AND (
(nvdproducts.company='linux' and nvdproducts.product='linux_kernel' AND nvdproducts.version IN (SELECT Kernel from Versions.VIEW_kernel))
OR (nvdproducts.company='apache' and nvdproducts.product='http_server' AND nvdproducts.version IN (SELECT Httpd from Versions.VIEW_httpd))
OR (nvdproducts.company='sendmail' and nvdproducts.product='sendmail' AND nvdproducts.version IN (SELECT Sendmail from Versions.VIEW_sendmail))
OR (nvdproducts.company='mysql' and nvdproducts.product='mysql' AND nvdproducts.version IN (SELECT Mysqld from Versions.VIEW_mysqld))
OR (nvdproducts.company='proftpd' and nvdproducts.product='proftpd' AND nvdproducts.version IN (SELECT Proftpd from Versions.VIEW_proftpd))
OR (nvdproducts.company='perl' and nvdproducts.product='perl' AND nvdproducts.version IN (SELECT Perl from Versions.VIEW_perl))
OR (nvdproducts.company='openssl' and nvdproducts.product='openssl' AND nvdproducts.version IN (SELECT Sslinuse from Versions.VIEW_sslinuse))
)
order by published;

这是结果。

+---------------+--------------------------------+----------+
| cve_id | date_format(published,'%Y-%m') | severity |
+---------------+--------------------------------+----------+
| CVE-2013-2128 | 2013-06 | MEDIUM |
| CVE-2013-1862 | 2013-06 | MEDIUM |
+---------------+--------------------------------+----------+
2 rows in set (0.00 sec)

最佳答案

将所有 nvdproducts 检查移至子查询中,以便您可以在其中使用 SELECT DISTINCT 来防止重复。

SELECT DATE_FORMAT(published,'%Y-%m'), severity, COUNT(severity)
FROM nvdcve
LEFT JOIN (SELECT distinct cve_id
FROM nvdproducts
WHERE (nvdproducts.company='linux' and nvdproducts.product='linux_kernel' AND nvdproducts.version IN (SELECT Kernel from Versions.VIEW_kernel))
OR (nvdproducts.company='apache' and nvdproducts.product='http_server' AND nvdproducts.version IN (SELECT Httpd from Versions.VIEW_httpd))
OR (nvdproducts.company='sendmail' and nvdproducts.product='sendmail' AND nvdproducts.version IN (SELECT Sendmail from Versions.VIEW_sendmail))
OR (nvdproducts.company='mysql' and nvdproducts.product='mysql' AND nvdproducts.version IN (SELECT Mysqld from Versions.VIEW_mysqld))
OR (nvdproducts.company='proftpd' and nvdproducts.product='proftpd' AND nvdproducts.version IN (SELECT Proftpd from Versions.VIEW_proftpd))
OR (nvdproducts.company='perl' and nvdproducts.product='perl' AND nvdproducts.version IN (SELECT Perl from Versions.VIEW_perl))
OR (nvdproducts.company='openssl' and nvdproducts.product='openssl' AND nvdproducts.version IN (SELECT Sslinuse from Versions.VIEW_sslinuse))
) nvdproducts
USING (cve_id)
WHERE published >= '2013-06-01'
GROUP BY DATE_FORMAT(published,'%Y-%m'), severity;

关于mysql连接后结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17492606/

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