简单的查询语句 select * from tabename
查询的列给别名 select column1 as AA ,column2 as BB from tablename where 条件
top 的使用 select top 3 * from tablename
select top 30 percent * from tablename (取前30%的数据)
Distinct 去除重复数据(DISTINCT是对查询出的整个结果集进行数据重复处理的,而不是针对某一个列)
聚合函数
通配符
空值处理
查询的列给别名 select column1 as AA ,column2 as BB from tablename where 条件
top 的使用 select top 3 * from tablename
select top 30 percent * from tablename (取前30%的数据)
Distinct 去除重复数据(DISTINCT是对查询出的整个结果集进行数据重复处理的,而不是针对某一个列)
聚合函数
•
MAX
(最大值)、
MIN
(最小值)、
AVG
(平均值)、
SUM
(和)、
COUNT
(数量
:
记录的条数。)
•
聚合函数对
null
值不计算。
•
如果一行的数据都是
null
,
count(*)
包含对空值行、重复行的统计。
select * from tabename where id in (2,3,4) //查询ID为2,3,4的记录
•
Between…and…
在之间
例:select * from tabename where id between 2 and 20 //查询ID为 2到20之间的记录
通配符
•
_
、
%
、
[]
、
^
•
^
只有
MSSQL Server
支持,其他
DBMS
用
not like
。
•
通配符
%
多字符匹配的通配符,它匹配任意次数(零或多个)出现的任意字符
•
通配符
_
单字符匹配,它匹配单个出现的字符
•
[]
只匹配一个字符 并且这个字符必须是
[]
范围内的
[0-9] [a-z]
•
not
与
like
一起使用:
notlike
….
•
要通配
_
、
%
、
[
、
^
这些字符怎么办?
[_]
、
[%]
、
[ [ ]
、
^(
不需要放到中括号里,因为
^
只有放到中括号中才认为是通配符
) 其实中括号就相当于转义字符
空值处理
•
数据库中,一个列如果没有指定值,那么值就为
null
,数据库中的
null
表示“
不知道
”,而不是表示没有。因此
select null+1
结果是
null
,因为“不知道”加
1
的结果还是“不知道”。
•
select* from score where
english
= null
;
•
select* from score where
english
!= null
;都没有任何返回结果,因为数据库也“不知道”。
•
SQL
中使用
is null
、
is not null
来进行空值判断:
select * from score where
english
is null
;
select * from score where
english
is notnull
;
•
ISNULL
(
check_expression
,
replacement_value
)