网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
08月05日漏签0天
广东信息科技职业...吧 关注:1,716贴子:17,711
  • 看贴

  • 图片

  • 吧主推荐

  • 游戏

  • 0回复贴,共1页
<<返回广东信息科...吧
>0< 加载中...

Server 知识汇集2 : Distinct / AND / OR

  • 只看楼主
  • 收藏

  • 回复
  • 可可冖正能量
  • 江湖少侠
    6
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
**第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;
```


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 0回复贴,共1页
<<返回广东信息科...吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示