简体   繁体   English

如何在 SQL WHERE 子句中过滤这些特定的行?

[英]How to filter these specific rows in SQL WHERE clause?

Say I have this table:说我有这张桌子:

ID |     A  |  B
1  |  blue  |  2
2  |  red   |  2
3  |  blue  |  1
4  |  red   |  1

Say I wanted all of column A except where blue is corresponding to a 2 in the B column.假设我想要 A 列的所有内容,除了蓝色对应于 B 列中的 2。

So essentially the results should give me rows 2,3,4 and not include 1.所以基本上结果应该给我第 2、3、4 行而不包括 1。

So far I have something like this:到目前为止,我有这样的事情:

SELECT *
FROM 
   Table
WHERE
   A IN ('blue','red')

So again, with this above query, it would include row 1 since it has blue.同样,使用上面的查询,它将包含第 1 行,因为它是蓝色的。 And for intents and purposes, let's say in column A there are more than those two colors but I just need those two, so I need that first WHERE statement.出于意图和目的,让我们说在 A 列中不止这两种颜色,但我只需要这两种,所以我需要第一个 WHERE 语句。 What's the simplest way to tell it to not include column A's blue when column B is 2?当 B 列是 2 时,告诉它不包括 A 列的蓝色的最简单方法是什么?

Thanks in advance.提前致谢。

What's the simplest way to tell it to not include column A's blue when column B is 2?当 B 列是 2 时,告诉它不包括 A 列的蓝色的最简单方法是什么?

This looks like:这看起来像:

select * 
from mytable
where a in ('blue', 'red') and not (a = 'blue' and b = 2)

You could also phrase this as:您也可以将其表述为:

where a = 'red' or (a = 'blue' and b <> 2)

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

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