Sam*_*ing 3 sql sql-server-2014
我如何在同一个SELECT SQL查询中使用WHERE和WHERE NOT?
例如,我可以有一个名为fruits的表.
ID | Name | Colour
-------------------------
1 | Strawberry | Red
2 | Apple | Red
3 | Grape | Red
4 | Banana | Yellow
Run Code Online (Sandbox Code Playgroud)
从这个表中,我可以执行一个SELECT * FROM [fruits] WHERE Colour = 'Red',它将检索以下内容:
ID | Name | Colour
-------------------------
1 | Strawberry | Red
2 | Apple | Red
3 | Grape | Red
Run Code Online (Sandbox Code Playgroud)
但是,我想Apple从上面的单个请求中排除,我可以使用一个WHERE NOT,但这将返回所有水果,包括Banana.
我将如何编写SQL查询,以便产生以下结果,选择特定的颜色,但不包括特定的水果:
ID | Name | Colour
-------------------------
1 | Strawberry | Red
3 | Grape | Red
Run Code Online (Sandbox Code Playgroud)
我正在使用MsSQL 2014,任何帮助将不胜感激.
你可以有多个子句where:
select *
from [fruits]
where Colour = 'Red'
and Name <> 'Apple'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2883 次 |
| 最近记录: |