Problemet er, at når du bruger GROUP BY
, kan du kun SELECT
aggregater og de kolonner, du har grupperet på.
invoices.id
er en kolonne, du forsøgte at vælge, men ikke grupperede. Jeg tror, du sandsynligvis vil tilføje denne kolonne til GROUP BY
klausul.
SELECT
invoices.id,
sum(grossTotal)-IFNULL(depositsCheck.previouslyPaid,0) as todayTotal,
depositsCheck.previouslyPaid, sum(grossTotal) as grossTotal
FROM `invoices`
LEFT JOIN (SELECT SUM(amount) as previouslyPaid, invNo
FROM deposits
GROUP BY invNo) depositsCheck ON depositsCheck.invNo=invoices.id
GROUP BY invoices.paymentType, invoices.id ORDER BY id DESC
For eksempeltabellerne, du gav, vil det sandsynligvis give:
id | paymentType | grossTotal | dateTime | previouslyPaid
1 | Cash | 1000 | UNIX TIME | 150
2 | Card | 1350 | UNIX TIME | 350
3 | Card | 1250 | UNIX TIME | 0
4 | Card | 750 | UNIX TIME | 0
Men generelt vil du have noget som:
id | paymentType | grossTotal | dateTime | previouslyPaid
1 | Cash | 1000 | UNIX TIME | 150
1 | Card | 1000 | UNIX TIME | 300
2 | Cash | 1350 | UNIX TIME | 350
2 | Card | 1350 | UNIX TIME | 350
Hvor du kan se ovenfor, for faktura 1, blev 150 betalt kontant, og 300 blev betalt med kort.