2017-03-07 40 views
0

感謝您查看我的文章,任何幫助/指導表示讚賞。我的SQL技能缺乏,我嘗試了幾種解決方案,但都沒有成功。無論如何,我需要創建一個查看以下查詢:替代查看以外的子查詢

CREATE VIEW open_orders AS 
SELECT 
t1.orderID, 
DATE_FORMAT(t1.orderDate,'%m-%d-%Y') AS orderDate, 
t6.colorName, 
t1.originID, 
t1.rackNumber, 
t2.locationNumber AS originNumber, 
t2.locationName AS originName, 
t3.locationName AS destinationName, 
t4.locationStatusName, 
COUNT(t5.orderID) AS totalCount 
FROM `order` AS t1 
JOIN `location` AS t2 ON t1.originID = t2.locationID 
JOIN `location` AS t3 ON t1.destinationID = t3.locationID 
JOIN `locationStatus` AS t4 ON t1.locationStatusID = t4.locationStatusID 
LEFT JOIN (SELECT * FROM `orderItem` WHERE `productStatusID` = 02 OR `productStatusID` = 03) AS t5 ON t1.orderID = t5.orderID 
JOIN `color` AS t6 ON t1.requestedColorID = t6.colorID 
WHERE t1.orderStatusID = 01 
GROUP BY t1.orderID 
ORDER BY t1.orderDate DESC, t1.orderID DESC; 

問題是與子查詢。因爲我無法在FROM語句中使用子查詢,所以我試圖使用VIEW。但是,表格很大,這種方法會導致太多的性能問題。

我敢肯定,有一種方法可以在不使用VIEW或子查詢的情況下完成此任務,但卻無法提供解決方案。

任何幫助/指導表示讚賞。

回答

2

你不需要子查詢。

... 
LEFT JOIN orderItem AS t5 
     ON t5.orderID = t1.orderID 
     AND t5.productStatusID IN (02,03) 
JOIN color ... 

這和你的查詢做的事情是一樣的,但是由於它避免了派生表,所以效率更高。

+0

完美!非常感謝Michael! – ObsDev