**第4节. 过滤数据**
1.Select Distinct子句
select distinct子句检索指定列列表中的唯一不同值,换句话说,它从结果集中删除列中的重复值
distinct子句将所有 NULL 值视为相同的值
语法如下所示:
```c
select distinct
column_name1,
column_name2 ,
...
from
table_name
```
A.distinct一个字段的示例
以下语句返回 customers 表中所有客户所在的所有城市(去重复):
```c
select distinct
city
from
sales.customers
order by
city
```
B.distinct多列示例
以下语句查找所有客户的不同城市和州
```c
select
city,
state
from
sales.customers
```
C.distinct带有null值示例
以下示例查找客户的不同(唯一)电话号码:
```c
select distinct
phone
from
sales.customers
order by
phone
```
2. SQL Server where子句
要从表中获取满足一行或多个条件的行,请使用 where子句,如下所示:
```c
select
select_list
from
table_name
where
search_condition
```
在 where子句中,指定搜索条件以过滤 from子句返回的行。 where子句仅返回导致搜索条件计算为 true的行。搜索条件是逻辑表达式或多个逻辑表达式的组合
通过使用简单的相等来查找行
以下语句检索类别为 id 为 1 的所有产品:
```c
select
product_id,
product_name,
category_id,
model_year,
list_price
from
production.products
where
category_id = 1
order by
list_price desc
```
3.and查找满足两个条件的行
and是一个逻辑运算符,用于组合两个布尔表达式
以下示例返回满足两个条件的产品: category_id 为 1 , model_year 为 2018 。 它使用逻辑运算符 and来组合这两个条件。
```c
select
product_id,
product_name,
category_id,
model_year,
list_price
from
production.products
where
category_id = 1 and model_year = 2018
order by
list_price desc
```
4. (>,=,<)使用比较运算符查找行
以下语句查找价格大于 300 且型号为 2018 的产品。
```c
select
product_id,
product_name,
category_id,
model_year,
list_price
from
production.products
where
list_price > 300 and model_year = 2018
order by
list_price desc
```
5.or 查找满足两个条件中的任何一个的行
or 是一个逻辑运算符,用于组合两个布尔表达式
在语句中使用多个逻辑运算符时,SQL Server将在 and运算符之后计算 or运算符。但是,可以使用括号更改评估顺序。
A. 使用or运算符示例
以下查询查找价格大于 3000 或型号为 2018 的产品。满足其中一个条件的任何产品都包含在结果集中。
```c
select
product_id,
product_name,
category_id,
model_year,
list_price
from
production.products
where
list_price > 300 or model_year = 2018
order by
list_price desc
```
B. 使用or和and运算符示例
请考虑以下示例:
```c
select
product_name,
brand_id,
list_price
from
production.products
where
brand_id = 3
or brand_id = 4
and list_price > 100
order by
brand_id desc;
```
1.Select Distinct子句
select distinct子句检索指定列列表中的唯一不同值,换句话说,它从结果集中删除列中的重复值
distinct子句将所有 NULL 值视为相同的值
语法如下所示:
```c
select distinct
column_name1,
column_name2 ,
...
from
table_name
```
A.distinct一个字段的示例
以下语句返回 customers 表中所有客户所在的所有城市(去重复):
```c
select distinct
city
from
sales.customers
order by
city
```
B.distinct多列示例
以下语句查找所有客户的不同城市和州
```c
select
city,
state
from
sales.customers
```
C.distinct带有null值示例
以下示例查找客户的不同(唯一)电话号码:
```c
select distinct
phone
from
sales.customers
order by
phone
```
2. SQL Server where子句
要从表中获取满足一行或多个条件的行,请使用 where子句,如下所示:
```c
select
select_list
from
table_name
where
search_condition
```
在 where子句中,指定搜索条件以过滤 from子句返回的行。 where子句仅返回导致搜索条件计算为 true的行。搜索条件是逻辑表达式或多个逻辑表达式的组合
通过使用简单的相等来查找行
以下语句检索类别为 id 为 1 的所有产品:
```c
select
product_id,
product_name,
category_id,
model_year,
list_price
from
production.products
where
category_id = 1
order by
list_price desc
```
3.and查找满足两个条件的行
and是一个逻辑运算符,用于组合两个布尔表达式
以下示例返回满足两个条件的产品: category_id 为 1 , model_year 为 2018 。 它使用逻辑运算符 and来组合这两个条件。
```c
select
product_id,
product_name,
category_id,
model_year,
list_price
from
production.products
where
category_id = 1 and model_year = 2018
order by
list_price desc
```
4. (>,=,<)使用比较运算符查找行
以下语句查找价格大于 300 且型号为 2018 的产品。
```c
select
product_id,
product_name,
category_id,
model_year,
list_price
from
production.products
where
list_price > 300 and model_year = 2018
order by
list_price desc
```
5.or 查找满足两个条件中的任何一个的行
or 是一个逻辑运算符,用于组合两个布尔表达式
在语句中使用多个逻辑运算符时,SQL Server将在 and运算符之后计算 or运算符。但是,可以使用括号更改评估顺序。
A. 使用or运算符示例
以下查询查找价格大于 3000 或型号为 2018 的产品。满足其中一个条件的任何产品都包含在结果集中。
```c
select
product_id,
product_name,
category_id,
model_year,
list_price
from
production.products
where
list_price > 300 or model_year = 2018
order by
list_price desc
```
B. 使用or和and运算符示例
请考虑以下示例:
```c
select
product_name,
brand_id,
list_price
from
production.products
where
brand_id = 3
or brand_id = 4
and list_price > 100
order by
brand_id desc;
```