SQLServer第三章:单表查询

查:是一个难点【select】查询产生的是一个“虚拟表”,执行原理:在原始表的基础之上,把满足条件的数据筛选出来,组成一个临时的结果集,响应到客户端。

创建一个简单的表做查询准备。

create database NetBarDB    --创建数据库create database 数据库名
go    --批处理(数据库无法自动运行下一句代码,需要加go来继续代码的运行)
use NetBarDB                --打开数据库
go

if exists(select * from sys.objects where name='PCInfo')
    begin
        drop table PCInfo
    end
create table PCInfo             --创建表:create table 表名
(
    PCId int primary key identity,    -- primary key 主键 identity(1,1) 自增不写括号数字默认1,1;意思是1开始后面的每个加1;
    PCname char(10) ,                    
    PCScore int not null 
)

给表批量循环添加数据

declare @i int  --sql里面的定义一个变量用    declare
set @i = 1        --给变量设置一个值用        set
while @i<=100        --while 条件循环
begin  --cast(表达式 as 转换类型),rand()是随机小数0-1(不包含0和1)的浮点型, 0至N-1之间floor(rand()*N),1至N之间ceiling(rand()*N),floor(<=表达式最大整数)ceiling(>=表达式最小整数),返回值类型都是与表达式相同
    insert into PCInfo values(''+convert(varchar,@i),cast( floor(rand()*101) as int)) --convert万能传换,这里int转varchar,@i是参数,0-100之间随机数浮点型转整型
    select @i=@i +1
end 

单表查询的常用语句

--1.查询表PCInfo所有数据:*表示所有
select * from PCInfo 
--2.查询指定的列和3种取别名的方法as
select PCId as 取别名,PCname 姓名,成绩=PCScore from PCInfo    --取别名的3种方式 可以接as  也可以不用接as直接空格取别名,另外取别名=列名,个人习惯用as
--3.条件查询:where子句接条件
select * from PCInfo where PCname='李1' --这里查PCScore=100的有哪些人。
select * from PCInfo where PCScore < 60
select * from PCInfo where PCScore>=60 and PCScore<=80 --and【和】 与下面这句查到一样的数据
select * from PCInfo where PCScore  between 60 and  80 --between【在...之间】在60和80之间 如果查以外的用 not between
select * from PCInfo where PCScore in (60,80)--in【里面的60或80】和 or【或】一样的意思,where sno not in('T123001','T123002')表示不是里面的这个条件
select * from PCInfo where PCScore=60 or PCScore=80
select * from PCInfo where PCScore=(select PCId from PCInfo where PCId='003')--案例查询:执行高效率(select 字段 from 表名 where 条件字段=条件值)赋值给PCScore字段
--4.判断空值:is
select * from PCInfo where PCname is not null --如果这个列数据不是空的就查询出来。这里声明 ''不等于null空值,所以会显示出来,在oracle数据库里面 ''等同于null
--5.模糊查询like:【通配符】  _单个字符匹配  %任意字符匹配  []范围内,[a-z]那么a-z任一都可以  [^]不在范围内
select * from PCInfo where PCname like '李_' --这种一个下划线表示一个未知字符,如果后面有多个就匹配不到,要加下划线,通常使用%来表示所有未知的字符
select * from PCInfo where PCname like '%李%'  
select * from PCInfo where PCId like '1[^1-5]%' --另外133[^1-5]% :[]范围内,^表示不再1-5之间,%表示后面接剩下的所有字符
--6.聚合函数:最小值,最大值,求和,平均值,行数。
select MAX(PCScore) as 最高分,  --SQL可以写换行,但在后台编程语言不允许这样子换行,只能一行写
        MIN(PCScore) as 最低分,
        SUM(PCScore) as 总分 ,
        AVG(PCScore) as 平均分,
        COUNT(PCScore) as '行数/*' 
from PCInfo
--注意:select MAX(PCScore),PCUse from PCInfo会报错。聚合函数与列名一起使用的前提是,语句中必须有group by【分组依据】;order by【排序依据】
--7.筛选行:分页会用到下面的这些,(这里说明top表示(上面/前面))
select  top 1 * from PCInfo order by PCId desc --第一名:select  top 1 * from 表名 order by 列名 desc  --这里加了order by 给列 PCId 排序功能,desc倒序,所以是倒数第一条数据
select top 3 PCname as 姓名,PCScore as 成绩 from PCInfo --表示(最上面/最前面)三条数据
select  top 50 percent* from PCInfo --查询前百分之50条数据:percent百分之几,  下面是不是50%表示一共数据的一半
--8.排序:order by【排序】:desc降序/asc升序(默认),对查询到的数据大小排序。
select * from PCInfo order by PCId desc --如果升序,desc改asc或者不写自动默认,直接删除desc即可升序排列。
--9.分组:group by
select COUNT(*) as 人数 ,PCScore as 成绩 from PCInfo group by PCScore --按成绩分组、查询成绩有多少人,这里只给两组数据,其实可以常用于,学号查每科明总成绩。
select COUNT(*) as 人数 ,PCScore as 成绩 from PCInfo where PCScore>=60 group by PCScore --只看60分以上的数据,【注意】分组前过滤用:where只能出现在grup by之前,后面不能跟聚合函数。
select COUNT(*) as 人数 ,PCScore as 成绩 from PCInfo where PCScore>=60 group by PCScore having count(*)<10 --分组后过滤用:having只能出现在group by之后,并且后面可以跟聚合函数。弥补where缺陷。
--10.newid()随机查看表n条数据
select top 10 * from PCInfo order by newid()  --随机查询10条数据

 case....end语句:其实是条件判断——【case..列名..when..数据..then..数据改变后的值..end as 列的别名,】

insert into PCInfo values('主机1',0),('主机2',1),('主机3',1),('主机4',0)
select pcid as 计算机编号,
        PCname as 计算机名称,
        case PCScore
            when 0 then '空闲'
            when 1 then '正在使用'
        end as 计算机状态
from pcinfo

 查询保密字段

select PCname from PCInfo --根据字段查询:查询到多少条数据返回每条数据内容
select 8 from PCInfo  --查询到多少条数据,每条数据显示都是8
select '******' from PCInfo  --每条数据显示都是******,有时候一些保密数据就需要这种查询方式。

 

posted @ 2022-09-15 01:04  Akai_啊凯  阅读(89)  评论(0编辑  收藏  举报