2017-03-07 10 views
0

私の投稿を見ていただきありがとうございました。私のSQLスキルは不足しており、成功なしにいくつかのソリューションを試しました。とにかく、次のクエリのためにVIEWを作成する必要があります。ビュー以外のサブクエリの代替手段

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

パーフェクト!マイケルありがとう! – ObsDev