表格规则:
RuleID RuleName 1 Rule1 2 Rule2 3 Rule3 4 Rule4
台式设备:
EquipmentID EquipmentName EquipmentCode 1 Equip1 E1 2 Equip2 E2 3 Equip3 E3
表RuleEquipment:
EquipmentRuleID RuleID EquipmentID 1 1 1 2 1 2 3 2 1 4 2 3 5 3 2
用户将使用一个且仅使用一个EquipmentCode进行查询。我想退回没有相关设备或配套设备的规则。
因此,如果用户查询E1,则结果集应为
RuleID RuleName 1 Rule1 2 Rule2 4 Rule4
我不能把手指放在WHERE子句上。有什么建议吗?
答案 0 :(得分:3)
SELECT * FROM Rule
WHERE Rule.ID NOT IN (SELECT RuleID FROM RuleEquipment)
OR Rule.ID IN
(
SELECT RuleID
FROM RuleEquipment
INNER JOIN Equipment ON RuleEquipment.EquipmentID = Equipment.EquipmentID
WHERE Equipment.EquipmentCode = @inputEquipmentCode
)
答案 1 :(得分:2)
select *
from [Rule] as R
where R.RuleID in (select RE.RuleID
from RuleEquipment as RE
inner join Equipment as E
on RE.EquipmentID = E.EquipmentID
where E.EquipmentCode = 'E1') or
R.RuleID not in (select RE.RuleID
from RuleEquipment as RE)
答案 2 :(得分:0)
SELECT Rule.*
FROM Rule
LEFT OUTER JOIN RuleEquipment ON RuleEquipment.RuleID = Rule.RuleID
Where Rule.RuleID = 1 or RuleID IS NULL