我正在使用Google Chart API構建產品銷售報告的折線圖,並且每天都基於在循環中運行相同的查詢來分離數據每天都使用UNION ALL作爲膠水。爲使用時間範圍提取數據優化MySQL查詢
E.g.全年的報告將重複查詢,使用UNION ALL 365次。
我猜這不是一個最好的做法 - 例如,如果任何人會如此善良,並指向我如何優化這個查詢的正確方向,我將不勝感激。
SELECT SUM(op.qty) AS qty
FROM uc_order_products op
LEFT JOIN uc_orders o ON o.order_id = op.order_id
LEFT JOIN node n ON n.nid = op.nid
LEFT JOIN node_type nt ON nt.type = n.type
WHERE (o.created > 1247695200) AND (o.created < 1247781600)
AND (o.order_status = 'completed')
AND (nt.type = 'book' OR nt.type = 'digital_movie')
UNION ALL
SELECT SUM(op.qty) AS qty
FROM uc_order_products op
LEFT JOIN uc_orders o ON o.order_id = op.order_id
LEFT JOIN node n ON n.nid = op.nid
LEFT JOIN node_type nt ON nt.type = n.type
WHERE (o.created > 1247781600) AND (o.created < 1247868000)
AND (o.order_status = 'completed')
AND (nt.type = 'book' OR nt.type = 'digital_movie')
UNION ALL
SELECT SUM(.......
感謝所有快速作出迴應!從jspcal查詢例如,我的查詢弄成這個樣子:
SELECT SUM(op.qty) AS qty, DATE_FORMAT(FROM_UNIXTIME(o.created),'%d-%m-%Y') AS day
FROM uc_order_products op
LEFT JOIN uc_orders o ON o.order_id = op.order_id
LEFT JOIN node n ON n.nid = op.nid
LEFT JOIN node_type nt ON nt.type = n.type
WHERE (o.created > 1247695200) AND (o.created < 1263596400)
AND (o.order_status = 'completed')
AND (nt.type = 'book' OR nt.type = 'digital_movie')
GROUP BY day
並與PHP,我結合一個完整的日期範圍陣列(相同排列的鍵與strtotime($data->day)
從查詢結果),以獲得天沒有銷售。
也許我失蹤了s這裏有什麼,但爲什麼你不會在創建時運行一個查詢>時間範圍的開始和時間範圍的結束? – 2010-01-16 11:53:13