2014-10-12 62 views
0

目標是從查詢2中減去查詢1並將其顯示在表中。我可以處理MySQL - 從單獨的查詢中減去

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 
)"); 

在此先感謝!

+0

你在你的子查詢缺少'GROUP BY User' ? – Barmar 2014-10-12 05:25:37

回答

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 
+0

你非常熟練。感謝您的幫助! – Mech 2014-10-12 17:08:01

0

首先使用兩個查詢的別名。然後運行一個查詢來減去這些查詢。

0

您需要使用單獨的SELECT查詢的所有數據,現在你只使用了一個SELECT語句,你需要重寫一遍這樣的 - SELECT( Select num1 FROM table1 ) - ( Select num1 FROM table2
),( Select num2 FROM table1 ) - ( Select num2 FROM table2
)