在子查询中计算唯一值

时间:2012-10-10 11:11:32

标签: php mysql count subquery

我正在尝试使用以下方法计算子查询返回的唯一值:

(SELECT COUNT(DISTINCT pm.id) FROM parts_manufacturers AS pm,
 parts_trading AS pt, enquiries 
 WHERE pm.id = pt.manufacturer AND pt.enquiryRef = enquiries.id 
 AND enquiries.entityRef = eo.id) AS manufacturers

但我收到了错误:

General error: 2014 Cannot execute queries while other unbuffered queries are active

我以前从未见过的。

我不能使用group by来消除重复项,因为它会使子查询返回超过1行。

我哪里出错了,如何在子查询中仅计算唯一值?


当我尝试在phpmyadmin中运行它时出现内存不足错误:

#5 - Out of memory (Needed 268435428 bytes)

以下是完整查询:

SELECT SQL_CALC_FOUND_ROWS eo.id, name, eia.county, $valueColumn $averageColumn
                        (SELECT CONCAT_WS(' ', entity_contacts.name, entity_contacts.lastName) FROM entity_contacts WHERE entityRef = eo.id AND isPrimary = 1 LIMIT 1) AS contact,
                        (SELECT ect.tel FROM entity_contacts AS ec, entity_contacts_telephones AS ect WHERE ect.contactRef = ec.id AND entityRef = eo.id AND ec.isPrimary = 1 LIMIT 1) AS tel,
                         countries.country AS countryName, CONCAT('images/flags/',LOWER(countryCode),'.gif') AS country, (SELECT COUNT(*) FROM enquiries WHERE entityRef = eo.id) AS enquired,
                         (SELECT COUNT(*) FROM enquiries, jobs WHERE enquiries.id = jobs.enquiryRef AND entityRef = eo.id AND jobs.stateRef != 5) AS jobCount,
                         ept.term AS paymentTerms, ei.type,
                         (SELECT COUNT(DISTINCT pm.id) as colname
                         FROM parts_manufacturers AS pm 
                        left outer join  parts_trading AS pt on pm.id = pt.manufacturer 
                        left outer join  enquiries on  pt.enquiryRef = enquiries.id
                         WHERE enquiries.entityRef = eo.id) AS manufacturers
                         FROM entity_details AS eo
                         LEFT JOIN entity_invoice_addresses AS eia ON eo.id = eia.entityRef AND eia.isPrimary = 1
                         LEFT JOIN countries ON eia.countryRef = countries.id
                         LEFT JOIN entity_industry_types AS ei ON ei.id = eo.industryTypeRef
                         LEFT JOIN entity_payment_terms AS ept ON eo.paymentTermsRef = ept.id,
                         users
                         WHERE eo.typeRef = 3
                         AND eo.deleted = 0
                         AND eo.ownerRef = users.id
                         LIMIT ?, ?

1 个答案:

答案 0 :(得分:0)

我不会从哪里来eo.id ..

所以,请澄清并回复:

    (SELECT COUNT(DISTINCT pm.id) as colname
         FROM parts_manufacturers AS pm 
        left outer join  parts_trading AS pt on pm.id = pt.manufacturer 
        left outer join  enquiries  on  pt.enquiryRef = enquiries.id
         WHERE enquiries.entityRef = eo.id) AS manufacturers

请尝试:我将用户更改为加入表格:--->

SELECT SQL_CALC_FOUND_ROWS eo.id, name, eia.county, $valueColumn $averageColumn
                        (SELECT CONCAT_WS(' ', entity_contacts.name, entity_contacts.lastName) FROM entity_contacts WHERE entityRef = eo.id AND isPrimary = 1 LIMIT 1) AS contact,
                        (SELECT ect.tel FROM entity_contacts AS ec, entity_contacts_telephones AS ect WHERE ect.contactRef = ec.id AND entityRef = eo.id AND ec.isPrimary = 1 LIMIT 1) AS tel,
                         countries.country AS countryName, CONCAT('images/flags/',LOWER(countryCode),'.gif') AS country, (SELECT COUNT(*) FROM enquiries WHERE entityRef = eo.id) AS enquired,
                         (SELECT COUNT(*) FROM enquiries, jobs WHERE enquiries.id = jobs.enquiryRef AND entityRef = eo.id AND jobs.stateRef != 5) AS jobCount,
                         ept.term AS paymentTerms, ei.type,
                         (SELECT COUNT(DISTINCT pm.id) as colname
                         FROM parts_manufacturers AS pm 
                        left outer join  parts_trading AS pt on pm.id = pt.manufacturer 
                        left outer join  enquiries on  pt.enquiryRef = enquiries.id
                         WHERE enquiries.entityRef = eo.id) AS manufacturers
                         FROM entity_details AS eo
                         LEFT outer JOIN entity_invoice_addresses AS eia ON eo.id = eia.entityRef AND eia.isPrimary = 1
                         LEFT outer JOIN countries ON eia.countryRef = countries.id
                         LEFT outer JOIN entity_industry_types AS ei ON ei.id = eo.industryTypeRef
                         LEFT outer JOIN entity_payment_terms AS ept ON eo.paymentTermsRef = ept.id,
                         LEFT outer JOIN users ON eo.ownerRef = users.id                          
                         WHERE eo.typeRef = 3
                         AND eo.deleted = 0                       
                         LIMIT ?, ?