我有一个使用多个COALESCE和JOIN的MySQL查询。我的目标是,从另一个表中获取值(如果它为空或在第一个表中不存在)。
我的问题是,如果它覆盖大量数据,则会产生错误:Fatal Error: Allowed memory size of ###### bytes exhausted. (tried to allocate...)
我试图创建一个视图表,但是即使我的PhpMyAdmin也无法处理它。
以下是查询:
SELECT COALESCE(i.`LastSyncDate`, o.`DateCreated`) AS TransDate,
COALESCE(CASE WHEN co.`Agent_ID` IS NOT NULL THEN UPPER(CONCAT(wp_agent.`first_name`, ' ', wp_agent.`last_name`)) ELSE '' END, CASE WHEN o.`StaffID` IS NOT NULL THEN UPPER(CONCAT(oss_agent.`first_name`, ' ', oss_agent.`last_name`)) ELSE '' END) AS AgentName,
COALESCE(UPPER(CONCAT(co.`LastName`, ' ', co.`FirstName`)), UPPER(CONCAT(u.`last_name`, ' ', u.`first_name`))) AS CustomerName,
COALESCE(UPPER(bb.`Name`), UPPER(bbb.`Name`)) AS ProductCode,
COALESCE(i.`Size`, o.`BoxSize`) AS dimension,
COALESCE(UPPER(r.`RouteCode`), UPPER(rr.`RouteCode`)) AS Destination,
COALESCE(UPPER(ocy.`Name`), UPPER(cy.`Name`)) AS City,
COALESCE(UPPER(op.`Name`), UPPER(p.`Name`)) AS Province,
COALESCE(UPPER(i.`BoxNumber`), UPPER(o.`BoxNumber`)) AS InvoiceNumber,
COALESCE((i.`Price` + i.`Tax`), o.`Price`) AS GrossAmount,
COALESCE((i.`AgentCom_AddPrice` + (i.`AgentCom_Factor` * i.`Price`)), 0) AS Commission,
COALESCE(i.`discount_price`, o.`promocode_discount`, 0) AS Discount,
COALESCE((i.`Price` - i.`discount_price` - (i.`AgentCom_AddPrice` + (i.`AgentCom_Factor` * i.`Price`))), (o.`Price` - o.`promocode_discount`), 0) AS NetAmount
FROM `cq_order_items` i LEFT OUTER JOIN `orders` o ON i.`BoxNumber` = o.`BoxNumber`
LEFT JOIN `cq_orders` co ON i.`OrderID` = co.`ID`
LEFT JOIN `users` u ON o.`CustomerID` = u.`id`
LEFT JOIN `cq_balikbayan_boxes` bb ON o.`BB_ID` = bb.`ID`
LEFT JOIN `cq_balikbayan_boxes` bbb ON i.`BB_ID` = bbb.`ID`
LEFT JOIN `cq_route_new` AS r ON i.`RouteID` = r.`ID`
LEFT JOIN `cq_route_new` AS rr ON o.`RouteID` = rr.`ID`
LEFT JOIN `addresses` a ON o.`Destination_Address` = a.`ID`
LEFT JOIN `cq_locality` loc ON a.`locality_id` = loc.`ID`
LEFT JOIN `cq_city` cy ON loc.`CityID` = cy.`ID`
LEFT JOIN `cq_city` ocy ON i.`CityID` = ocy.`ID`
LEFT JOIN `cq_provice` p ON cy.`provinceID` = p.`ID`
LEFT JOIN `cq_provice` op ON ocy.`provinceID` = op.`ID`
LEFT JOIN `users` wp_agent ON co.`Agent_ID` = wp_agent.`WP_ID`
LEFT JOIN `users` oss_agent ON o.`StaffID` = oss_agent.`id`
#WHERE i.`BoxNumber` IN (".$invoice_nums.")
ORDER BY AgentName ASC, InvoiceNumber ASC
有办法减轻它的负担吗?