0

2 つの作業クエリがあります。

1 つ目は、製品リストを返します。

$sql = "
   SELECT a.stockID, a.stockCatID, a.stockName, a.stockCode, a.stockCatCode,
   CONCAT_WS(' » ', d.stockCatName, c.stockCatName, b.stockCatName) AS stockPath,
   a.stockName AS stockTitle, a.stockID AS uniStock
   FROM stockcards a
   LEFT OUTER JOIN stockcategories b
     ON a.stockCatID = b.stockCatID
   LEFT OUTER JOIN stockcategories c
     ON b.stockParentCat = c.stockCatID
   LEFT  OUTER JOIN stockcategories d
     ON c.stockParentCat = d.stockCatID ";

2番目は基本的に、受信した合計と送信した合計の差を返します。これは残りの数量です。

SELECT DISTINCT (COALESCE(o.totalReceived, 0) + COALESCE(p.totalSent, 0)) as RemainingStock
 FROM deliverydetails k 
 INNER JOIN stockcards l ON k.stockID= l.stockID
 LEFT JOIN
     (
         SELECT m.stockID, SUM(m.dQuantity) totalReceived
         FROM deliverydetails m
         WHERE m.dQuantity > 0
         GROUP BY m.stockID
     )
     o ON k.stockID = o.stockID
 LEFT JOIN
     (
         SELECT n.stockID, SUM(n.dQuantity) totalSent
         FROM deliverydetails n
         WHERE n.dQuantity < 0
         GROUP BY n.stockID
     )
     p ON k.stockID = p.stockID

残りの数量を表示するには、最初のクエリに新しい列を追加する必要があります。しかし、この 2 つを結合することはできませんでした。ヒントをありがとう。

4

1 に答える 1

0

2 番目のクエリは、stockID ごとに正と負の dQuantities を合計してそれらを加算しているように見えるので、最初のクエリにこの小さな変更を加えるだけで十分だと思います。

SELECT a.stockID, a.stockCatID, a.stockName, a.stockCode, a.stockCatCode,
   CONCAT_WS(' » ', d.stockCatName, c.stockCatName, b.stockCatName) AS stockPath,
   a.stockName AS stockTitle, a.stockID AS uniStock,
   dd.RemainingStock AS RemainingStock 
   FROM stockcards a
   LEFT OUTER JOIN stockcategories b
     ON a.stockCatID = b.stockCatID
   LEFT OUTER JOIN stockcategories c
     ON b.stockParentCat = c.stockCatID
   LEFT  OUTER JOIN stockcategories d
     ON c.stockParentCat = d.stockCatID
   LEFT OUTER JOIN 
     (SELECT stockID, SUM(dQuantity) AS RemainingStock
     FROM deliverydetails
     GROUP BY stockID) AS dd
   ON dd.stockID = a.stockID
于 2013-01-20T17:30:37.203 に答える