如何在MySQL中使用单个语句减去

时间:2010-01-14 21:26:12

标签: mysql

如何减去此查询的结果:

SELECT COUNT(Laptops) FROM (SELECT aaaUser.FIRST_NAME AS User,COUNT(workstation.WORKSTATIONNAME) AS Laptops FROM SystemInfo workstation 
LEFT JOIN Resources resource ON workstation.WORKSTATIONID=resource.RESOURCEID 
LEFT JOIN ResourceOwner rOwner ON resource.RESOURCEID=rOwner.RESOURCEID 
LEFT JOIN ResourceAssociation rToAsset ON rOwner.RESOURCEOWNERID=rToAsset.RESOURCEOWNERID 
LEFT JOIN SDUser sdUser ON rOwner.USERID=sdUser.USERID 
LEFT JOIN AaaUser aaaUser ON sdUser.USERID=aaaUser.USER_ID WHERE ( aaaUser.FIRST_NAME IS NOT NULL)  AND ISSERVER=0 
GROUP BY "User"
HAVING Laptops > 1
ORDER BY Laptops DESC) AS e

来自这个:

SELECT SUM(Laptops) FROM (SELECT aaaUser.FIRST_NAME AS User,COUNT(workstation.WORKSTATIONNAME) AS Laptops FROM SystemInfo workstation 
LEFT JOIN Resources resource ON workstation.WORKSTATIONID=resource.RESOURCEID 
LEFT JOIN ResourceOwner rOwner ON resource.RESOURCEID=rOwner.RESOURCEID 
LEFT JOIN ResourceAssociation rToAsset ON rOwner.RESOURCEOWNERID=rToAsset.RESOURCEOWNERID 
LEFT JOIN SDUser sdUser ON rOwner.USERID=sdUser.USERID 
LEFT JOIN AaaUser aaaUser ON sdUser.USERID=aaaUser.USER_ID WHERE ( aaaUser.FIRST_NAME IS NOT NULL)  AND ISSERVER=0 
GROUP BY "User"
HAVING Laptops > 1
ORDER BY Laptops DESC) AS e

虽然只使用一个查询?

我正在尝试在不支持运行多个查询的产品中创建报表 - 因此临时表不是一个选项。

提前致谢,

3 个答案:

答案 0 :(得分:4)

除非我遗漏了什么,否则你不能这样做吗?

SELECT SUM(Laptops) - COUNT(Laptops)
FROM (SELECT aaaUser.FIRST_NAME AS User,COUNT(workstation.WORKSTATIONNAME) AS Laptops FROM SystemInfo workstation 
LEFT JOIN Resources resource ON workstation.WORKSTATIONID=resource.RESOURCEID 
LEFT JOIN ResourceOwner rOwner ON resource.RESOURCEID=rOwner.RESOURCEID 
LEFT JOIN ResourceAssociation rToAsset ON rOwner.RESOURCEOWNERID=rToAsset.RESOURCEOWNERID 
LEFT JOIN SDUser sdUser ON rOwner.USERID=sdUser.USERID 
LEFT JOIN AaaUser aaaUser ON sdUser.USERID=aaaUser.USER_ID WHERE ( aaaUser.FIRST_NAME IS NOT NULL)  AND ISSERVER=0 
GROUP BY "User"
HAVING Laptops > 1
ORDER BY Laptops DESC) AS e

答案 1 :(得分:2)

SELECT SUM(Laptops) - COUNT(Laptops)
from ...

因为您的汇总函数将是应用于GROUP的结果的最后一项。

答案 2 :(得分:0)

SELECT (SELECT query 1 ... ...) - (SELECT query 2 ... ...) AS 'value';