where 关键字
Comparison conditions(比较条件):=,>,<,>=,<=,<>
between * and *
Where 语句中使用
or 或 and
Like "%" 或 "_" 通配符
in 或 not in
is null 或 is not null
_代替了正则里面的?号
百分符号夹住表示模糊搜索,因为前后都放宽了
数据类型为int 则后面等于号不用加上单引号
between....and
int:整数型可以直接输入。
string:字符串需用单引号。
where 列表 is not null ——挑出列表中不为空值的数据。
两级以上的运输、or、and需要用括号
count 统计表中数量,distinct去除表中重复的部分。
select count(distinct(列名)) ——统计列表中去重之后的数量。
where 中多个条件用and连接
where+某列名 in(‘某1’,‘某2’,‘某3’):选出某列中名字是某1,某2,和某3的行。
not in (‘某某’):是某列中不是某某的行。
where+列名 like‘%某名称%’:能选出该列所有含某名称的行。
‘_某名称’:第一个字母不确定的某行。
where条件1and条件2:两个条件都符合的行。
Orderdate between‘年-月-日’and‘月/日/年’:两种日期写法
where+条件:从哪里选出符合此条件的行。
字符型需要单引号括起来。
WHERE语句中使用 is null 或 is not null
如图:is not null
WHERE 语句中使用in 或 not in
class not in('H')--same as--class <> 'H'
WHERE 语句中使用=,>,<,>=,<=,<>(不等)
SELECT * FROM Sales.SalesOrderHeader
WHERE SalesOrderNumber='so43670'
int:整数型(不需要单引号)sting:字符型 (需要加单引号)
WHERE语句中使用 or或and
SELECT SalesOrderID,OrderDate,SalesPersonID,TotalDue as TaotalSales
FROM Sales.SalesOrderHearder
WHERE SalesPersonID=275 and TotalDue>5000 and OrderDate >= '2005-08-01' and OrderDate < '1/1/2006'
and条件都必须满足
or满足任何一个条件即可
WHERE语句中使用 Like “%”或“_”通配符(wildcard) 如图:
like'Mountain’(在xx表所有列中名为Mountion的)
like'%Mountion%'(%表示在Mountion的前后可以有也可以没有任何字符)
like'_ountian%'("_"表示不确定字符)
时间的两种表示方式
1、where 有条件的从表中选取数据
select 列名 from 表名 where 列 运算符 值
运算符:=、>、<、>=、<=
字符型varchar加'',int型不用
2、and 运算符(连接的条件同时满足)
and 可在 where 子语句中把两个及以上的条件结合起来
A and B and C,between '2015-08-01' and '1/1/2016'--2种年月日的表示
or 运算符(连接的条件满足其一即可)
3、Where语句中使用like “%”“_”通配符
%模糊查询,where name like'%mountain%',前后可以有字符的name
where name like'mountain%',前面无字符的name
_模糊搜索 ,where name like'_ountain%'
4、in或者not in
where name in(属性一,属性二,属性三...),查询出满足3种属性的数据
where class not in ('H') -- 另一种用法<> 'H'(不等于H)
5、is null或者is not null
where size is not null
6、and和or同时使用时,and优先级高,先执行and语句再执行or语句
可以把or语句放在()中,优先级高于and
or and 混合用法
and or 用法
is not null不为NULL 值时查询出来
not in ('H')用法 class里面没有H这个字符串
nvarchar,null
size in 用法
in 用法
'_ountain%' _一个字母任意调换
%Mountain% Mountain前后可有可无字符串
通配符 % 进行模糊查询
精确化数据库区间
between用法
where 里面可以用and