【SqlServer系列】001、SELECT语句

发布时间 2023-09-26 11:14:45作者: stfzhuang
 

SELECT语句

 

1、  1基本的select语句
1、1、1从表中选择列
select a,b from table
1、  1、2选择所有列
select * from table
 

 

1、  2 where子句
1、2、1 null值

注意: 在搜索条件中有null数据时可能会出现unknown值。

null值不同于空白或0,只表示值未知。

并且,两个null值并不相等,不产生unknown值的话是不能做比较的。

 

准备数据:

##创建表
CREATE TABLE EducationType 
(
    EducationTypeID int not null,
    EducationTypeName VARCHAR(40) not null,
    EducationTypeRemark VARCHAR(40)  null
)
##插入数据
insert INTO EducationType (EducationTypeID,EducationTypeName) values (1,'zhuangjun1')
insert INTO EducationType (EducationTypeID,EducationTypeName) values (2,'zhuangjun2')
insert INTO EducationType (EducationTypeID,EducationTypeName,EducationTypeRemark) values (3,'zhuangjun2','非空')
insert INTO EducationType (EducationTypeID,EducationTypeName,EducationTypeRemark) values (4,'zhuangjun1','非空')

##创建子表
CREATE TABLE EducationType_Sub 
(
    EducationTypeID_Sub int not null,
    EducationTypeName_Sub VARCHAR(40) not null,
    EducationTypeRemark_Sub VARCHAR(40)  null,
    EducationTypeID int not null
)
##插入子表数据
insert INTO EducationType_Sub (EducationTypeID_Sub,EducationTypeName_Sub,EducationTypeID) values (1,'zhuangjun1_sub',1)
insert INTO EducationType_Sub (EducationTypeID_Sub,EducationTypeName_Sub,EducationTypeID) values (2,'zhuangjun2_sub',1)
insert INTO EducationType_Sub (EducationTypeID_Sub,EducationTypeName_Sub,EducationTypeRemark_Sub,EducationTypeID) values (3,'zhuangjun2','非空_sub',3)
insert INTO EducationType_Sub (EducationTypeID_Sub,EducationTypeName_Sub,EducationTypeRemark_Sub,EducationTypeID) values (4,'zhuangjun1','非空_sub',4)


##查询数据
SELECT * from  EducationType where EducationTypeRemark = null

SELECT * from  EducationType where EducationTypeRemark is null

SELECT * from  EducationType where EducationTypeRemark is not null

SELECT * from EducationType a ,EducationType b where a.EducationTypeRemark = b.EducationTypeRemark

null不能用等于号,需要用is null 或者is not null

 

 

null并不能用来比较,也就是说两个null并不相等,如果null=null,那么下图应该显示出8条数据

 

 

1、2、2 指定结果集中返回的列
select * from table where title = 'Ms'
1、  2、3 组合搜索条件
select * from table where title = 'Ms' and LastName = 'Antrim'
1、2、4 否定搜索条件
#第一种方式
select * from table where title != 'Ms'

#第二种方式
select * from table where not title = 'Ms'
1、2、5 保持where子句无歧义

你可以在单个where子句中使用多个运算符(and,or,not)。但在组合它们的时候应该考虑执行顺序。避免歧义。

如:

select * from table where title = 'Ms'  and (LastName = 'Antrim' 
or mark = 'test')
 
 
1、  3 使用运算符和表达式

概览:

 

1、3、1 在日期范围搜索中使用between
select *  from table where shipdate between '2023-09-15' and '2023-09-20'
1、3、2 使用比较运算符
select *  from table where cost > 1100
1、  3、3 基于一组值返回行
select *  from table where color  in ('silver','balck','red')
1、  3、4 like和通配符结合使用

如果想查询通配符本身呢? 使用escape

##值里面本身就有%符号
insert INTO EducationType (EducationTypeID,EducationTypeName,EducationTypeRemark) values (4,'zhuangjun1 % test','非空')

##单引号中的斜线放在escape命令之后。这表示斜线符号为前面like表达式字符串中的转义字符。如果转义字符出现在搜索条件中的下划线之前,那么它将被视为文本值而不是通配符
SELECT * from  EducationType where  EducationTypeName like '%/%%' escape '/'

 

1、  3、5 声明变量及为变量赋值
declare @AddressLine1 nvarchar(60)
set @AddressLine1 = 'zhuangjun1'

SELECT * FROM [dbo].[EducationType] where EducationTypeName = @AddressLine1

 

 

 

1、  4 数据分组
1、4、1 使用group by 分组

SELECT EducationTypeName from EducationType group by  EducationTypeName

SELECT EducationTypeName,count(1) from EducationType group by  EducationTypeName
1、  4、2 使用group by all
declare @AddressLine1 nvarchar(60)
set @AddressLine1 = 'zhuangjun1'

SELECT EducationTypeName,count(1) from EducationType where EducationTypeName = @AddressLine1  group by  all EducationTypeName

1、  4、3 使用having 选择性地查询分组的数据

select语句中的having子句允许你在使用了group by 或聚合值的查询中指定一个搜索条件。

SELECT EducationTypeName,count(1) from EducationType   group by   EducationTypeName

SELECT EducationTypeName,count(1) from EducationType   group by   EducationTypeName having count(1)>1

 

 

 

1、  5 对结果排序
1、  5、1 使用order by 子句
SELECT * from EducationType  ORDER BY EducationTypeID asc
SELECT * from EducationType  ORDER BY EducationTypeID desc

 

 

 

1、5、2 top关键字

TOP关键字允许根据定义的行的数量或百分比查询出开始的N行。查询如何排序也将影响返回的这些行。可以用来做分页查询。

##具体多少条
SELECT top 2 * from EducationType  ORDER BY EducationTypeID asc
SELECT top 2 * from EducationType  ORDER BY EducationTypeID desc
##百分比
SELECT top 20 percent * from EducationType  ORDER BY EducationTypeID desc
SELECT top 60 percent * from EducationType  ORDER BY EducationTypeID desc

 

 

 

 

1、  6 select子句技术
1、6、1 使用distinct消除重复值
SELECT distinct hireDate from employee

确认只有在真正需要或必要时使用distinct ,因为他会使具有较大结果集的查询速度减慢。

1、  6、2  在聚合函数中使用distinct
select AVG(distinct listPrice) from product

它先返回唯一的一组零售价,然后计算它们的平均值

1、  6、3 使用列别名
select color as 'Grouped Color' , avg(distinct listPrice) as 'average distinct list price' ,avg(listPrice) 'average list price' from product group by color

可以使用关键字as ,也可以省略。

1、6、4 使用select创建条件脚本
select column_name + 'IS NULL AND' information_schema.columns where table_name = 'employee' order by ordinal_position

这个可以作为一种where后缀去加在别的SQL里面

1、  6、5 字符串拼接

字符串拼接操作通过使用+运算符连接两个表达式来实现。

select 'the ' + name + 'is only '+ convert(varchar(25),p.listPrice)+ '!' from product 
1、  6、6 使用select创建逗号分隔的列表
##创建变量
declare @Shifts varchar(20) = ''
##赋值
select @Shifts = @Shifts + name + ',' from shift 
##显示变量
select @Shifts
1、  6、7 使用into子句
##创建表,并且插入查询到的数据
select businessid,name,persionid into store_archive from store
##创建表,但是不插入任何数据,因为where 1=0永远不成立
select businessid,name,persionid into store_archive2 from store where 1=0