我正在为Mac OSX客户端使用名为MunkiReport的报告工具。该工具使用一个MySQL数据库,该数据库带有一个名为inventoryitem
的表,该表如下所示(我省略了一些列):
| id | serial_number | path
| 1 | C01 | Applications/Application01.app
| 2 | C01 | Applications/Application02.app
| 3 | C01 | Applications/Application03.app
| 4 | C02 | Applications/Application01.app
| 5 | C02 | Applications/Application03.app
| 6 | C02 | Applications/Application04.app
现在,我想列出所有同时安装了Application01和Application03的序列号,我这样做是这样的:
SELECT `serial_number`
FROM `inventoryitem`
WHERE `path` IN ('Applications/Application01.app','Applications/Application03.app')
GROUP BY `serial_number`
HAVING COUNT(`path`)=2
输出为:
| serial_number |
| C01 |
| C02 |
但是,我真正想要的是列出确实安装了Application03但没有安装Application02的所有序列号...
我已经尝试了几件事,但是我做错了。我以为可能是子查询?首先列出Application03,然后从该数据列出所有没有Application02的序列号。
我对MySQL有点新手,这让我有些困惑。
最佳答案
您可以尝试在HAVING子句中汇总条件表达式-如下所示:
SELECT `serial_number`
FROM `inventoryitem`
WHERE `path` IN ('Applications/Application02.app','Applications/Application03.app')
GROUP BY `serial_number`
HAVING COUNT(case `path` when 'Applications/Application03.app' then 1 end)=1
AND COUNT(case `path` when 'Applications/Application02.app' then 1 end)=0