0

我必须表 ProdBiscuit As tb 和 StockData As sd ,我必须在 if (sd.status>0 AND sd.prodid = tb.id AND sd.matcuisine = 的条件下获得 StockData (quantite) 中数量的总和3)

这是我的 sql 查询

 SELECT tb.id, tb.nom, tb.proddate, tb.qty, tb.stockrecno 
 FROM ProdBiscuit AS tb
 JOIN 

(SELECT id, prodid, matcuisine, status, SUM(quantite) AS rq FROM StockData) AS sd
 ON (tb.id = sd.prodid AND sd.status > 0 AND sd.matcuisine = 3) LIMIT 25 OFFSET @Myid

这根本没有给我任何行?

ProdBiscuit 中只有 3 行,Stockdata 中只有 11 行,StockData 中只有 2 行符合条件。

如图所示,只有两行给出条件。

两张桌子的图片

我的查询有什么问题?

PS:图像上的绿线显示了我的查询中的条件。

4

2 回答 2

2

请检查这个。如果您没有得到想要的结果,请访问 SQLFiddle.com 并创建包含数据的示例表,以便更轻松地找出您想要的结果。

SELECT tb.id, tb.nom, tb.proddate, tb.qty, tb.stockrecno, SUM(sd.quanite)
FROM ProdBiscuit AS tb
JOIN StockData AS sd
  ON tb.id = sd.prodid
WHERE sd.status > 0 AND sd.matcuisine = 3
GROUP BY tb.id, tb.nom, tb.proddate, tb.qty, tb.stockrecno
于 2013-11-11T22:23:33.023 回答
0
SELECT tb.id, tb.nom, tb.proddate, tb.qty, tb.stockrecno, SUM(quantite)  As rq

FROM ProdBiscuit AS tb 

JOIN StockData AS sd  ON (tb.id = sd.prodid AND sd.status > 0 AND sd.matcuisine = 3)
OR (tb.id = sd.prodid AND sd.status = 0 AND sd.matcuisine = 3)  
GROUP BY tb.id, tb.nom, tb.proddate, tb.qty, tb.stockrecno

这个查询给了我这个

ID  NOM                    QTY      RQ
1   Biscuit Chocolat       100      100
2   Biscuit Moutarde       120      226
3   Biscuit Pain Epice     100      100

在@AgRizzo 的帮助下,我做了一些修改并添加了精确的显示:

SELECT tb.id, tb.nom, tb.proddate, tb.qty, tb.stockrecno, 

SUM(IF((status>0 AND tb.id=sd.prodid),quantite,0))  As rq FROM ProdBiscuit AS tb 

JOIN StockData AS sd  ON (tb.id = sd.prodid AND sd.status > 0 AND sd.matcuisine = 3)
OR (tb.id = sd.prodid AND sd.status = 0 AND sd.matcuisine = 3)  
GROUP BY tb.id, tb.nom, tb.proddate, tb.qty, tb.stockrecno



ID  NOM                    QTY      RQ
1   Biscuit Chocolat       100      100
2   Biscuit Moutarde       120       14
3   Biscuit Pain Epice     100      100

Code for displaying the "rq" is:

QtyLst.Items.Add((Convert.ToInt16(reader["qty"])- Convert.ToInt16(reader["rq"])).ToString());
于 2013-11-12T00:30:51.627 回答