简体   繁体   English

排除基于多个变量的记录

[英]Excluding records based on multiple variables

SQL noob here. SQL noob在这里。 I'm using MS Access to query a table of streetlight data to exclude lights of a certain size and only where the fixture type is a variant of "Cobra". 我正在使用MS Access查询路灯数据表,以排除特定大小的光,并且仅在灯具类型是“眼镜蛇”的变体的情况下。 Is anyone kind enough to give me some guidance on what I might be doing wrong? 有谁愿意对我可能做错的事情给我一些指导?

I can produce the lights I need to be excluded with: 我可以生产需要排除的灯:

SELECT * 
FROM Lighting 
WHERE Lighting.TYPE_FIXTURE LIKE '*cobra*' 
AND Lighting.SIZ < '16'

Simple enough. 很简单。 I thought this would be easy to tweak a little bit to get it to exclude these instead of produce them. 我认为这很容易进行一些调整,以排除它们而不是产生它们。

SELECT * 
FROM Lighting
WHERE Lighting.TYPE_FIXTURE NOT IN ('*Cobra*') 
AND Lighting.SIZ NOT IN ('10', '15')

This code excludes the lights I need to be excluded, but also every other type of light that is 100 or 150w. 此代码排除了我需要排除的灯,但排除了其他所有类型的100或150w的灯。 For the life of me, I can't figure out how to exclude ONLY Cobra lights 150w or less. 为了我的一生,我想不出如何排除150w以下的眼镜蛇灯。

Through extensive internet research, I tried using an IIF expression to produce a temporary column that would have a value of 0 or 1, depending on if it needed to be excluded or not, 通过广泛的互联网研究,我尝试使用IIF表达式生成一个临时列,该列的值为0或1,具体取决于是否需要排除它,

SELECT *
    ,IIf(Lighting.TYPE_FIXTURE LIKE "*cobra*" AND Lighting.SIZ < 15, 0, 1) AS IncludeFlag
INTO #MyTempTable
FROM Lighting

SELECT *
FROM #MyTempTable
WHERE IncludeFlag = 1

but my only result is an error message that I don't know how to resolve. 但是我唯一的结果是一条错误消息,我不知道该如何解决。 I've spent all day on this one query and I'm starting to pull my hair out!! 我已经花了整整一天时间在这个查询上,而且我开始拔头发了!!

Try this... 尝试这个...

SELECT * 
FROM Lighting 
WHERE (Lighting.TYPE_FIXTURE NOT LIKE '*cobra*' 
AND Lighting.SIZ < '16')

Converting from a TRUE and TRUE to a NOT(TRUE and TRUE) involves distributing the NOT inside the brackets and switch the AND to an OR. 从TRUE和TRUE转换为NOT(TRUE和TRUE)涉及将NOT分布在方括号内,并将AND切换为OR。

The following should give you the results you want: 以下内容应为您提供所需的结果:

SELECT *
FROM Lighting
WHERE Lighting.TYPE_FIXTURE Not Like '*cobra*' OR Lighting.SIZ>='16';

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

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