目標是從查詢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
)");
在此先感謝!
你在你的子查詢缺少'GROUP BY User' ? – Barmar 2014-10-12 05:25:37