MySQL - 从单独的查询中减去

时间:2014-10-12 05:23:45

标签: php mysql sql

目标是从查询2中减去查询1并将其显示在表中。我可以处理

while ($row = mysql_fetch_array($result)) {

请求的一部分,但我无法让以下工作。这意味着要花几个月的销售额来看看它们的比较。

$result = mysql_query("SELECT (
Select
    User, 
    SUM(COMPUTERS) as COMPUTERSTotal,
    SUM(LAPTOPCOMPUTERS) as LAPTOPCOMPUTERSTotal,
    SUM(PCCOMPUTERS) as PCCOMPUTERSTotal,
    SUM(KEYBOARDSUnits) as KEYBOARDSTotal, 
    SUM(MOUSEUnits) as MOUSETotal, 
    SUM(PRINTERSUnits) as PRINTERSTotal, 
    SUM(MISCUnits) as MISCTotal,
    SUM(PACKAGES) as PACKAGESTotal,
    (round(SUM(LAPTOPCOMPUTERS) / SUM(COMPUTERS) * 100,2)) as LAPTOPPercentage,
    (round(SUM(PACKAGES) / SUM(PCCOMPUTERS) * 100,2)) as PACKAGEPercentage,
    SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) as PERIPHERALTotal,
    round(SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) / SUM(COMPUTERS),2) as PERIPHERALPERSALE,
    ((SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) / SUM(COMPUTERS) * (SUM(COMPUTERS) * 1)) * (round(SUM(LAPTOPCOMPUTERS) / SUM(COMPUTERS) * 100,2))) as Ratio
FROM my_db
WHERE YEAR(`Created`) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(`Created`) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
)
-
(
Select
    User,   
    SUM(COMPUTERS) as COMPUTERSTotal,
    SUM(LAPTOPCOMPUTERS) as LAPTOPCOMPUTERSTotal,
    SUM(PCCOMPUTERS) as PCCOMPUTERSTotal,
    SUM(KEYBOARDSUnits) as KEYBOARDSTotal, 
    SUM(MOUSEUnits) as MOUSETotal, 
    SUM(PRINTERSUnits) as PRINTERSTotal, 
    SUM(MISCUnits) as MISCTotal, 
    SUM(PACKAGES) as PACKAGESTotal,
    (round(SUM(LAPTOPCOMPUTERS) / SUM(COMPUTERS) * 100,2)) as LAPTOPPercentage,
    (round(SUM(PACKAGES) / SUM(PCCOMPUTERS) * 100,2)) as PACKAGEPercentage,
    SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) as PERIPHERALTotal,
    round(SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) / SUM(COMPUTERS),2) as PERIPHERALPERSALE,
    ((SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) / SUM(COMPUTERS) * (SUM(COMPUTERS) * 1)) * (round(SUM(LAPTOPCOMPUTERS) / SUM(COMPUTERS) * 100,2))) as Ratio
FROM my_db
WHERE `Created` >= CURDATE() - INTERVAL DAY(NOW()) - 1 DAY
)");

提前致谢!

3 个答案:

答案 0 :(得分:2)

您不能将一行数据用作表达式中的值。如果子查询返回单个元素(包含一列的1行),则只能将子查询用作值。对于您正在执行的操作,您需要加入子查询,然后减去每列。

我认为您在查询中也遗漏了GROUP BY User;如果您没有按用户计算总数,则选择该列毫无意义。

SELECT lastmonth.User, 
       lastmonth.COMPUTERSTotal - lastday.COMPUTERSTotal AS COMPUTERSTotal,
       lastmonth.LAPTOPCOMPUTERSTotal - lastday.COMPUTERSTotal AS LAPTOPCOMPUTERSTotal,
       ...
FROM (Select
        User, 
        SUM(COMPUTERS) as COMPUTERSTotal,
        SUM(LAPTOPCOMPUTERS) as LAPTOPCOMPUTERSTotal,
        SUM(PCCOMPUTERS) as PCCOMPUTERSTotal,
        SUM(KEYBOARDSUnits) as KEYBOARDSTotal, 
        SUM(MOUSEUnits) as MOUSETotal, 
        SUM(PRINTERSUnits) as PRINTERSTotal, 
        SUM(MISCUnits) as MISCTotal,
        SUM(PACKAGES) as PACKAGESTotal,
        (round(SUM(LAPTOPCOMPUTERS) / SUM(COMPUTERS) * 100,2)) as LAPTOPPercentage,
        (round(SUM(PACKAGES) / SUM(PCCOMPUTERS) * 100,2)) as PACKAGEPercentage,
        SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) as PERIPHERALTotal,
        round(SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) / SUM(COMPUTERS),2) as PERIPHERALPERSALE,
        ((SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) / SUM(COMPUTERS) * (SUM(COMPUTERS) * 1)) * (round(SUM(LAPTOPCOMPUTERS) / SUM(COMPUTERS) * 100,2))) as Ratio
    FROM my_db
    WHERE YEAR(`Created`) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
    AND MONTH(`Created`) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
    GROUP BY User
    ) AS lastmonth
JOIN (Select
        User,   
        SUM(COMPUTERS) as COMPUTERSTotal,
        SUM(LAPTOPCOMPUTERS) as LAPTOPCOMPUTERSTotal,
        SUM(PCCOMPUTERS) as PCCOMPUTERSTotal,
        SUM(KEYBOARDSUnits) as KEYBOARDSTotal, 
        SUM(MOUSEUnits) as MOUSETotal, 
        SUM(PRINTERSUnits) as PRINTERSTotal, 
        SUM(MISCUnits) as MISCTotal, 
        SUM(PACKAGES) as PACKAGESTotal,
        (round(SUM(LAPTOPCOMPUTERS) / SUM(COMPUTERS) * 100,2)) as LAPTOPPercentage,
        (round(SUM(PACKAGES) / SUM(PCCOMPUTERS) * 100,2)) as PACKAGEPercentage,
        SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) as PERIPHERALTotal,
        round(SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) / SUM(COMPUTERS),2) as PERIPHERALPERSALE,
        ((SUM(KEYBOARDSUnits + MOUSEUnits + PRINTERSUnits + MISCUnits) / SUM(COMPUTERS) * (SUM(COMPUTERS) * 1)) * (round(SUM(LAPTOPCOMPUTERS) / SUM(COMPUTERS) * 100,2))) as Ratio
    FROM my_db
    WHERE `Created` >= CURDATE() - INTERVAL DAY(NOW()) - 1 DAY
    GROUP BY User
    ) AS lastday
ON lastmonth.User = lastday.User

答案 1 :(得分:0)

首先使用两个查询的别名。然后运行一个减去这些查询的查询。

答案 2 :(得分:0)

您需要对所有数据使用单独的选择查询,现在您只使用了一个select语句,您需要像这样重写它 - SELECT( Select num1 FROM table1 ) - ( Select num1 FROM table2
),( Select num2 FROM table1 ) - ( Select num2 FROM table2
)