查询以比较多个值

时间:2014-05-30 14:57:11

标签: mysql sql

我从SQL开始,我遇到了一些我不知道的东西。 我有一个包含事件的数据库(比如某些桌面出现的问题的描述)以及包含已安装软件和桌面硬件的其他表。

我试图制作一个SQL查询,只显示安装了常用软件的工作站上的事件。就像我有10次服务器重置事件一样,我想知道这些服务器有多少安装软件。

目前我有这个:

-- Query to find out how many times each omschriving (description) occurs
-- and to show only those that occur more than one time
SELECT incidents.omschrijving, COUNT(omschrijving) AS Aantal
  FROM hondsrug_db.incidents incidents
WHERE   (hardware.hardwareID = installed_software.hardwareID)
   AND (incidents.hardwareID = hardware.hardwareID)
GROUP BY incidents.omschrijving
HAVING COUNT(omschrijving) > 1
ORDER BY Aantal DESC

在此ERD http://nl.tinypic.com/view.php?pic=oayr9i&s=8#.U4ibbfl_sbg中可见,安装的软件表包含2个外键。这是因为硬件组件可以安装多个软件组件。对于此表,这意味着hardwareID可以在installed_software表中多次出现,但每次都使用另一个softwareID。 现在我想知道,如果您有10个服务器事件,您如何找到这么多服务器拥有相同的软件集?

提前谢谢

2 个答案:

答案 0 :(得分:0)

以下查询加入incidentsinstalled_softwaresoftware表格。

对于每个事件,从hardwareID表中查找服务器(softwareID)和软件(installed_software)。从naam表中查找软件名称(software)。

SELECT software.softwareID, software.naam, count(installed_software.hardwareID)
FROM hondsrug_db.incidents incidents
INNER JOIN  hondsrug_db.installed_software installed_software
ON incidents.hardwareID = hardwareID
INNER JOIN hondsrug_db.software software
ON installed_software.softwareID = software.softwareID
GROUP BY installed_software.softwareID;

答案 1 :(得分:0)

我认为'omschrijving'包含事件的类型。您必须使用hardware_ID作为密钥将此表连接到软件表。您还可以创建另一个联接,不仅显示软件ID,还显示名称。 (我滑了这一步)


    SELECT incidents.omschrijving, installed_software.software_id, COUNT(omschrijving) AS Aantal
      FROM hondsrug_db.incidents incidents
    INNER JOIN hondsrug_db.installed_software installed_software 
    ON installed_software.hardwareID=incidents.hardwareID
    GROUP BY incidents.omschrijving, software_id
    ORDER BY COUNT(omschrijving) DESC

出于分析目的,我会将结果导出到excel并使用交叉表来分析它。

相关问题