简体   繁体   English

MS-ACCESS / SQL - 如何在多个条件下应用 where 子句

[英]MS-ACCESS / SQL - How to apply where clause in multiple conditions

SELECT Stock.*
FROM Stock
WHERE (
(
(Stock.ComputerPartNumber) In (SELECT [ComputerPartNumber] FROM [Stock] As Tmp GROUP BY [ComputerPartNumber] HAVING Count(*)=2)
) 
AND 
(
(Stock.EquipmentName)="EquipmentA" Or (Stock.EquipmentName)="EquipmentB")
) 
OR (
(
(Stock.ComputerPartNumber) In (SELECT [ComputerPartNumber] FROM [Stock] As Tmp GROUP BY [ComputerPartNumber] HAVING Count(*)=1)
) 
AND (
(Stock.EquipmentName)="EquipmentA" Or (Stock.EquipmentName)="EquipmentB"
)
);

I am using the above SQL to achieve below 3 items:-我正在使用上面的 SQL 来实现以下 3 项:-

  1. Find out all of the ComputerPartNumber which used by EquipmentA and/or EquipmentB only找出仅由 EquipmentA 和/或 EquipmentB 使用的所有 ComputerPartNumber
  2. Filter out the query result if the ComputerPartNumber used by equipment other than EquipmentA and EquipmentB.如果ComputerPartNumber被EquipmentA和EquipmentB以外的设备使用,则过滤掉查询结果。
  3. If the ComputerPartNumber is used by both EquipmentA and EquipmentC, filter out the result also.如果 EquipmentA 和 EquipmentC 都使用 ComputerPartNumber,则也过滤掉结果。

However the item 3 cannot be filtered out successfully.但是无法成功过滤出第 3 项。 What should I do in order to achieve the item3?为了实现item3,我应该怎么做? Table and Query snapshots are attached.附加了表和查询快照。 Thanks in advance!提前致谢!

Table桌子

Query询问

What you need to do is to check if the total number of times a part is used in all pieces of Equipment is equal to the total number of times a part is used by either Equipment A or B:您需要做的是检查一个部件在所有设备中使用的总次数是否等于设备 A 或 B 使用部件的总次数:

SELECT S.StorageID, S.ComputerPartNumber, S.EquipmentName, S.Result
FROM Stock AS S
WHERE 
(SELECT COUNT(*) FROM Stock AS S1 WHERE S1.ComputerPartNumber=S.ComputerPartNumber)
=(SELECT COUNT(*) FROM Stock AS S2 WHERE S2.ComputerPartNumber=S.ComputerPartNumber AND S2.EquipmentName IN("EquipmentA","EquipmentB"))

Regards,问候,

You can use not exists :您可以使用not exists

select s.*
from stock as s
where not exists (select 1
                  from stock as s2
                  where s2.ComputerPartNumber = s.ComputerPartNumber and
                        s2.EquipmentName not in ("EquipmentA", "EquipmentB")
                 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM