如何从表中选择另一个表中另一列等于我的参数值的数据?

时间:2017-01-05 17:49:01

标签: c# sql-server

我是初学者,我想:   - 当total quant equal = 0且第二个表中的status字段等于process时,从下表中获取recivedID table 1 - stockTBL

table 2 - recivedTBL

表示此订单中的所有产品均为0且订单状态=流程时 我想返回recivedID

这是我的代码:

SELECT s.recivedID FROM stockTBL s
JOIN recivedTBL r ON r.recivedID = s.recivedID
WHERE r.status = @STATUS 
GROUP BY s.recivedID 
HAVING (SUM(s.quant) = 0)

2 个答案:

答案 0 :(得分:1)

因此,您希望选择所有没有stockTBL关联的receivedTBL记录。

SELECT s.recivedID, SUM(s.quant) 
  FROM stockTBL s
  JOIN recivedTBL r ON r.recivedID = s.recivedID
 WHERE r.status = @STATUS 
 GROUP BY s.recivedID 
HAVING (SUM(s.quant) = 0) -- sum stockTBL

答案 1 :(得分:0)

我假设您需要SQL代码来实现这一目标:

SELECT T1.recivedID
FROM TABLE1 T1
INNER JOIN TABLE2 T2 ON T1.recivedID = t2.recivedID
WHERE T1.quant = 0
AND T2.status = @status