我的查询有问题。
我想在我的 WHERE 中使用 SUM,但它一直给我无效使用组函数。
AND SUM((invoices_out.net+invoices_out.vat)/currencies.rateVsPound)
>
(SELECT SUM(jobs_payments_accounts.amount/currencies.rateVsPound)
FROM jobs_payments_accounts, jobs, enquiries, currencies
WHERE jobs_payments_accounts.jobRef = jobs.id
AND jobs_payments_accounts.currencyRef = currencies.id
AND enquiries.id = jobs.enquiryRef
AND enquiries.entityRef = ed.id)
> 的右侧工作完美,但我怎样才能实现我想要用左侧做的事情?
这是完整的子查询:
IF(ed.paymentTermsRef = 3,
(SELECT SUM((invoices_out.net+invoices_out.vat)/currencies.rateVsPound)
FROM enquiries, jobs, (SELECT * FROM invoices_out_reference GROUP BY invoiceRef) AS iorUnique,
(SELECT enquiryRef, sellingCurrency FROM parts_trading GROUP BY enquiryRef) AS PTU,
currencies,
invoices_out
WHERE enquiries.entityRef = ed.id
AND enquiries.id = jobs.enquiryRef
AND PTU.enquiryRef = enquiries.id
AND PTU.sellingCurrency = currencies.id
AND jobs.id = iorUnique.jobRef
AND iorUnique.invoiceRef = invoices_out.id
AND invoices_out.paid = 0
AND SUM((invoices_out.net+invoices_out.vat)/currencies.rateVsPound)
>
(SELECT SUM(jobs_payments_accounts.amount/currencies.rateVsPound)
FROM jobs_payments_accounts, jobs, enquiries, currencies
WHERE jobs_payments_accounts.jobRef = jobs.id
AND jobs_payments_accounts.currencyRef = currencies.id
AND enquiries.id = jobs.enquiryRef
AND enquiries.entityRef = ed.id)
#AND jobs.id NOT IN
# (SELECT jobRef FROM jobs_payments_accounts)
# AND jobs.id NOT IN
# (SELECT jobRef FROM jobs_payments_advance)
AND jobs.id IN
(SELECT jobRef FROM invoices_out_reference)
AND DATEDIFF(NOW(), invoices_out.date) >= 30),
NULL
)
尝试使用 IS NULL 实现您的建议,但这会使查询速度变慢。
仍然有很多麻烦,希望我能尽快破解它。