无效使用组功能

时间:2012-05-23 06:24:29

标签: php mysql

大部分查询都放在变量中,但出于这个问题的目的,我已将它们包含在查询中。保证金是其上方所有别名查询的总和($ margin)。

我收到错误:

SQLSTATE [HY000]:常规错误:1111第117行的company-performance-control.php中无效使用组功能

这是我的问题:

$margin = $invoicesOut.'-'.$costs.'+'.$creditsIn.'-'.$creditsOut ;

$result = $dbh->query("SELECT CONCAT(MONTH(invoices_out.date), '/', YEAR(invoices_out.date)) AS theDate,
                           SUM(COALESCE(
                           (SELECT SUM(invoices_out.net/rate)
                                FROM invoices_out, (SELECT jobRef, invoiceRef FROM invoices_out_reference GROUP BY invoiceRef) AS unique_references
                                WHERE unique_references.invoiceRef = invoices_out.id
                                AND unique_references.jobRef = jobs.id
                           ),
                       0)) AS invoiced,
                           SUM((SELECT SUM((quantity*parts_trading.sellingNet)/currencies.rateVsPound)
                FROM parts_trading, currencies
                WHERE parts_trading.sellingCurrency = currencies.id
                AND parts_trading.enquiryRef = enquiries.id
                AND jobs.id NOT IN
                    (SELECT DISTINCT jobRef FROM invoices_out_reference)
            )
            +
            COALESCE(
            (SELECT SUM(enquiries_custom_fees.feeAmountNet/currencies.rateVsPound)
                FROM enquiries_custom_fees, parts_trading, currencies
                WHERE enquiries_custom_fees.enquiryRef = enquiries.id
                AND enquiries_custom_fees.enquiryRef = parts_trading.enquiryRef
                AND parts_trading.sellingCurrency = currencies.id
            ),
            0
            )) AS pending,
                           SUM(COALESCE(
                (SELECT SUM(net)
                FROM credit_notes_supplier, parts_trading_buying
                WHERE credit_notes_supplier.partRef = parts_trading_buying.id
                AND parts_trading_buying.enquiryRef = enquiries.id)
              ,0
              )) AS creditsIn,
                           SUM(COALESCE(
                (SELECT SUM(net)
                FROM credit_notes_customer, parts_trading
                WHERE credit_notes_customer.partRef = parts_trading.id
                AND parts_trading.enquiryRef = enquiries.id)
               ,0
               )) AS creditsOut,
                           SUM(((SELECT SUM(qty*(parts_trading_buying.buyingNet/currencies.rateVsPound))
                FROM parts_trading_buying, currencies
                WHERE parts_trading_buying.buyingCurrency = currencies.id
                AND parts_trading_buying.enquiryRef = enquiries.id
          )
          +
          COALESCE(
          (SELECT SUM(parts_trading_buying_charges.feeAmountNet)
               FROM parts_trading_buying_charges, parts_trading_buying
               WHERE parts_trading_buying_charges.partRef = parts_trading_buying.id
               AND parts_trading_buying.enquiryRef = enquiries.id
          )
          , 0))) AS costs,
                           SUM($margin) AS margin
                           FROM jobs, enquiries, invoices_out,
                           (SELECT jobRef, invoiceRef
                                FROM invoices_out_reference
                                GROUP BY invoiceRef
                           ) AS unique_invoice_refs
                           WHERE enquiries.id = jobs.enquiryRef
                           AND unique_invoice_refs.invoiceRef = invoices_out.id
                           AND jobs.id = unique_invoice_refs.jobRef
                           AND jobs.stateRef != 1
                           AND jobs.stateRef != 5
                           GROUP BY YEAR(invoices_out.date), MONTH(invoices_out.date)") ;

我把它缩小到这些:

SUM($creditsIn) AS creditsIn
SUM($creditsOut) AS creditsOut

定义为:

$creditsIn = 'COALESCE(
                    (SELECT SUM(net)
                    FROM credit_notes_supplier, parts_trading_buying
                    WHERE credit_notes_supplier.partRef = parts_trading_buying.id
                    AND parts_trading_buying.enquiryRef = enquiries.id)
                  ,0
                  )' ;

    $creditsOut = 'COALESCE(
                    (SELECT SUM(net)
                    FROM credit_notes_customer, parts_trading
                    WHERE credit_notes_customer.partRef = parts_trading.id
                    AND parts_trading.enquiryRef = enquiries.id)
                   ,0
                   )' ;

1 个答案:

答案 0 :(得分:1)

真是个大问题!

排除故障。只需一次删除一个子查询,并确定导致此问题的查询。