0
因此,我期望在兩年內每個客戶都有發票存在的總髮票。所以,我有兩年內發票存在時按年份計算總計
SELECT Sum((tbl_invoice_details.quantity
- ifnull(tbl_invoice_details.quantity_returned, 0)
) * tbl_invoice_details.price) AS dollarsSold,
tbl_customers.NAME,
cust_ID AS customer_id,
YEAR (tbl_invoice.invoice_date) AS invoiceYear
FROM `tbl_invoice_details`
INNER JOIN `tbl_invoice`
ON `tbl_invoice_details`.`invoice_ID` = `tbl_invoice`.`invoice_ID`
INNER JOIN `tbl_customers`
ON `tbl_customers`.`customer_ID` = `tbl_invoice`.`cust_ID`
WHERE MONTH (`invoice_date`) = '2'
AND (YEAR (`invoice_date`) = '2017'
OR
YEAR (`invoice_date`) = '2016')
AND EXISTS (SELECT *
FROM tbl_invoice
WHERE cust_ID = customer_id
AND MONTH (`invoice_date`) = '2'
AND YEAR (`invoice_date`) = '2016')
GROUP BY MONTH (tbl_invoice.invoice_date),
invoiceYear,
`tbl_customers`.`name`,
customer_id
ORDER BY name
那麼,如何將其限制爲同時具有2016和2017年我與EXISTS
嘗試過,但是,這只是成功的一半在二月發票的客戶。