使用T-SQL语言查询数据,它属于数据查询语言(DQL:Data Query Language)。
一、使用T-SQL语言查询数据之单表查询。
假如有下面一张表,以它举例说明:
create table ProductInfo
(
Id int identity(10001, 1) not null,
ProductNo varchar(50) not null,
ProductName nvarchar(100) not null,
ProductTypeId int not null,
ProductPrice decimal(18, 2) not null,
ProductCount int not null,
ProductRemark nvarchar(150) null
)
go
1.1 查询所有列的数据。
语法:
select * from 表名 where 条件表达式
例如:
select * from ProductInfo where Id = 10001
注意:
* 代表一张表里的所有列,不推荐这种方式,毕竟内存有限。
1.2 查询部分列的数据。
语法:
select 列名1, 列名2, 列名3...... from 表名 where 条件表达式
例如:
select ProductNo, ProductName, ProductPrice from ProductInfo where Id = 10001
1.3 给列取别名。
有三种方式:使用as关键字,使用空格,使用等号。
语法:
select
列名1 as 别名1,
列名2 别名2,
别名3 = 列名3,
......
from
表名
where
条件表达式
例如:
select
ProductNo as 产品编号,
ProductName 产品名称,
产品价格 = ProductPrice
from
ProductInfo
where
Id = 10001
注意:
不推荐使用中文别名,因为不符合规范。建议在后台代码里进行别名更改。
1.4 排序
语法:
select
列名1, 列名2, 列名3......
from
表名
where
条件表达式
order by
列名1, 列名2 [asc | desc]
例如:
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
Id = 10001
order by
ProductTypeId, ProductNo desc
注意:
如果没有指定排序方式,默认按主键的升序方式排序。
ASC:升序,从小到大。 DESC:降序,从大到小。
不管是否有where条件,group by分组,order by永远放在最后。
二、使用T-SQL语言查询数据之模糊查询。
在where子条件语句中like的应用:
2.1 使用关键字 "%" 匹配0个、1个或多个字符。
有3种匹配方式:
2.1.1 任意匹配(只要包含XXX字符就会被匹配上)
语法:
select
列名1, 列名2, 列名3......
from
表名
where
列名1 like '%XXX%'
例如:
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like '%连衣裙%'
2.1.2 开头匹配(只要以XXX字符开头就会被匹配上)
语法:
select
列名1, 列名2, 列名3......
from
表名
where
列名1 like 'XXX%'
例如:
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like '雪糕%'
2.1.3 结尾匹配(只要以XXX字符结尾就会被匹配上)
语法:
select
列名1, 列名2, 列名3......
from
表名
where
列名1 like '%XXX'
例如:
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like '%龙虾'
2.2 使用关键字 "_" 匹配单个字符。
这种模糊查询会限制表达式的字符长度,"_" 相当于占位符,只有符合相应格式的内容才会被查询出来。
语法:
select
列名1, 列名2, 列名3......
from
表名
where
列名1 like '_XX_X_'
例如:
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like '雪纺_'
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like '__大龙虾'
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like '_包邮__退款_'
2.3 使用关键字 "[]" 进行范围匹配,匹配“在”括号中所有候选字符中的单个字符。
语法:
select
列名1, 列名2, 列名3......
from
表名
where
列名1 like 'XXX[XX]XXX'
例如:
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like '雪[纺|糕]' -- 匹配包含“雪糕”、“雪纺”的字符
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like '[澳洲|美国]大龙虾' -- 匹配包含“澳洲大龙虾”、“美国大龙虾”的字符
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like 'sand[adkw]ich' -- 第5个字符匹配“a、d、k、w”中的任意一个
-- 另一种写法: ProductName like 'sand[a|d|k|w]ich'
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like 'sand[u-w]ich' -- 第5个字符匹配英文字符“u”至“w”中的任意一个(也就是u、v、w)
2.4 使用关键字 "[^]" 进行范围匹配,匹配“不在”括号中所有候选字符中的单个字符。
语法:
select
列名1, 列名2, 列名3......
from
表名
where
列名1 like 'XXX[^XX]XXX'
例如:
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like '雪[^纺|糕]' -- 匹配不包含“雪糕”、“雪纺”的字符
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like '[^澳洲|美国]大龙虾' -- 匹配不包含“澳洲大龙虾”、“美国大龙虾”的字符
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like 'sand[^adkw]ich' -- 第5个字符不匹配“a、d、k、w”中的任意一个
-- 另一种写法: ProductName like 'sand[^a|d|k|w]ich'
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductName like 'sand[^u-w]ich' -- 第5个字符不匹配英文字符“u”至“w”中的任意一个(也就是不匹配u、v、w)
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductNo like '10002[^5-9]' -- 最后一个字符不匹配数字5-9中的任意一个
三、使用T-SQL语言查询数据之范围查询。
3.1 查询前面N条数据或者按百分比来查询数据。
3.1.1 查询前面N条数据。
语法:
select
top N
列名1, 列名2, 列名3......
from
表名
例如:
select
top 20
ProductNo, ProductName, ProductPrice
from
ProductInfo
3.1.2 按百分比来查询数据。
语法:
select
top N percent
列名1, 列名2, 列名3......
from
表名
例如:
select
top 50 percent
ProductNo, ProductName, ProductPrice
from
ProductInfo
3.2 在where条件子句中设置查询范围。
3.2.1 使用比较运算符。
如: “=”、 “<>”、 “>”、 “<”、 “>=”、 “<=” 。
语法:
select
列名1, 列名2, 列名3......
from
表名
where
条件表达式
例如:
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
Id >= 10001 and
Id <= 10099
3.2.2 使用 "in"、 "not in" 关键字。
语法:
select
列名1, 列名2, 列名3......
from
表名
where
列名1 in (数值范围) / 列名1 not in (数值范围)
例如:
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
Id in (10001, 10077, 10099) / Id not in (10001, 10077, 10099)
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
ProductTypeId in
(
select
ProductTypeId
from
ProductTypeInfo
where
ProductTypeId > 8
)
注意:
"in"、 "not in" 后面跟着的范围,除了写具体的数值外,还可以写其他表查询后的数据集合。
3.2.3 使用“between...and...”关键字。
语法:
select
列名1, 列名2, 列名3......
from
表名
where
列名1 between 数值1 and 数值2
例如:
select
ProductNo, ProductName, ProductPrice
from
ProductInfo
where
Id between 10010 and 10099 -- 等价于 Id >= 10010 and Id <= 10099
注意:
"列名1 between 数值1 and 数值2" 等价于 "列名1 >= 数值1 and 列名1 <= 数值2"。
推荐使用“between...and...”关键字进行区间范围查询,效率比使用“>= and <=”运算符高。
四、使用T-SQL语言查询数据之聚合函数。
聚合函数:对一组值进行计算并返回单一值,经常与select语句中的group by结合使用。
有常见的5种聚合函数,分别是:
4.1 count 返回记录的数量。
语法:
select count(*) as 别名 from 表名
例如:
select count(*) as RecordCount from ProductInfo -- 低效返回记录数量
select count(Id) as RecordCount from ProductInfo
select count(1) as RecordCount from ProductInfo -- 高效返回记录数量
注意:
在ProductInfo表中并不存在数字为1的列名,这种行为叫伪造列。
当表中数据特别庞大时,count(1)这种方式执行效率特别高,而count(*)的执行效率非常低。
4.2 sum 求和。
语法:
select sum(列名1) as 别名 from 表名
例如:
select sum(ProductCount) as TotalProductCount from ProductInfo -- 求产品数量的总和
4.3 avg 求平均值。
语法:
select avg(列名1) as 别名 from 表名
例如:
select avg(ProductPrice) as AvgProductPrice from ProductInfo -- 求产品价格的平均值
4.4 max 取最大值。
语法:
select max(列名1) as 别名 from 表名
例如:
select max(ProductPrice) as MaxProductPrice from ProductInfo -- 取最高产品价格
4.5 min 取最小值。
语法:
select min(列名1) as 别名 from 表名
例如:
select min(ProductPrice) as MinProductPrice from ProductInfo -- 取最低产品价格
五、使用T-SQL语言查询数据之分组查询。
语法:
select
列名1, count(1) as 别名
from
表名
where
条件表达式 -- 分组前进行范围筛选
group by
分组表达式
having
筛选表达式 -- 分组后进行范围筛选
order by -- 最终排序
列名1 [asc | desc]
例如:
select
ProductTypeId,
count(1) as EachProductTypeCount -- 取别名
from
ProductInfo
where
Id > 10030 and
ProductPrice > 199
group by
ProductTypeId -- 要想不出错,列要么出现在group by条件中,要么出现在聚合函数中
having
ProductTypeId > 3 -- 分完组后表里只有2个列:ProductTypeId、EachProductTypeCount
order by
ProductTypeId desc
注意:
select 出现的列名,要么包含在聚合函数中,要么出现在group by子句中,否则会报错。
SQL Server数据库基础:使用T-SQL语言查询数据之单表查询、模糊查询、范围查询、聚合函数、分组查询。
于 2022-08-02 11:51:02 首次发布