我正在尝试使用以下方法计算子查询返回的唯一值:
(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 ?, ?