欢迎投稿

今日深度:

学习 SQL Server (4) : 多种查询语句,

学习 SQL Server (4) : 多种查询语句,



--======================查询=========================
--select * from 表名

--简单查询---查询指定列 select 指定的列 from 表名
select id,name,sex,age from Employee
--注:select age*14 from Employee

--简单查询--查询前几行信息 select top 几行 * from 表名
select top 5 * from Employee

--查询前6名员工的姓名、性别、年龄
select top 6 name,sex,age from Employee

--简单查询--带条件的查询
--查询年龄大于25的员工
select * from Employee where age>25
--查询年龄大于等于25 小于等于30的员工
select * from Employee where age>=25 and age<=30
--查询年龄大于25 的员工的姓名 年龄 性别 生日
select name,age,sex,birthday from Employee where age>25
select * from Employee where age>25 and sex=0

---=============对查询结果进行排序==============
--按年龄升序显示信息
select * from Employee order by age asc
--按年龄降序显示
select * from Employee order by age desc

--简单查询-----对查询结果去除重复数据 关键字 distinct
select distinct age from Employee

--简单查询------给查询的列起别名 as as可以省略
select a.Age 年龄,a.Name 姓名 from Employee a

 

 

---=========================模糊查询=============================
--关键字是 like
--模糊查询要配合 通配符 进行
-- 通配符有四种 :% — [] [^]


--通配符 %:表示任意匹配 0个 或多个字符
-- 查询姓'王'的员工信息
select * from Employee where name like '王%' --第一个字为'王'的
--查询名字以'三'结尾的
select *from Employee where name like '%王' --最后一个字为'王'的
-- 名字包含'三'的
select * from Employee where name like '%王%' --有'王'的


--通配符 _:表示任意一个字符
select * from Employee where name like '张_'
select * from Employee where name like '张__' --- 一个'张'加任意一个字, 一个_ 为一个字
select * from Employee where name like '_张_'


-- [] 和 [^] 的使用
--1) [...]: 表示在指定范围内
select * from Employee where Age like '2[0,2,1]' --十位为2 且 个位是0或2或1 的

--2) [ ^...]: 表示不在指定范围内
select * from Employee where Age like '[^2,1][6,7]' --不是二十几 或不是十几 且 个位是6或7的


--Between ... and ...表示范围
select * from Employee where age between 20 and 30 --年龄在20和30之间


-- in 的查询 表示包含在其中的
select * from Employee where age in (15,20,25,30) --查找属于符合其中之一的数据
-- not in 表示不包含在其中的
select * from Employee where age not in (15,20,25,30) --查找出了这几个的其他的数据
--【难题】 查询没有员工的部门信息
select * from Department where Id not in (select distinct DepartmentId from Employee)


-- 对 Null 值进行查询 is null
--查询没有身份证号的员工
select * from Employee where CardId is Null
select * from Employee where CardId is not Null


--排序 order by 升序【默认】asc / 降序desc
select * from Employee order by age desc

 

----==================聚合函数:对数据表的数据进行一些简单地运算,将结果显示出来=========
---- sum() 求和函数
select sum(age) from Employee

---- avg() 平均值
select avg(age) from Employee

---- max() 最大值
select max(age) from Employee

---- min() 最小值
select min(age) from Employee

---- count() 计数器
select count(age) from Employee

--=================================================================================================
--======== 聚合函数不能与普通列放在一起被查询出来,除非都用聚合函数 或者用在分组查询中 ==========
--======== 如 select max(age),name from 表名 错误 ==========
----====== select max(age),count(name) from 表名 正确 ==========
--=================================================================================================

--【难题:查询年龄最大的员工的信息】
-- 法一 嵌套法
select * from Employee where age=(select max(age) from Employee)
--法二 排序法
select top 1 * from Employee order by age desc

 

--=================== 分组统计查询 关键字:group by 分组列===================

-- 分别=分组统计出男员工 女员工的平均年龄
select avg(age) from Employee group by Sex

--分组统计出每个部门的员工数量
select count(name),DepartmentId from Employee group by DepartmentId --注意两个DepartmentId 前后一致性

--==== 对分组查询设置条件 关键字 having
--分别统计出每个部门的员工数量 只显示员工数量大于3人的部门
select count(name),DepartmentId from Employee group by DepartmentId having count(name)>3

 

 

--=======================================嵌套查询========================================================

--查询年龄大于平均年龄的 员工信息
select * from Employee where Age>(select avg(Age) from Employee)

--查询年龄大于平均年龄的 员工人数
select count(1) from Employee where Age>(select avg(Age) from Employee)

--查询至少有一个员工的部门信息 【重点】
--查询部门信息 where 部门编号 in (查询员工信息的部门编号)
select * from Department where Id in(select distinct DepartmentId from Employee)
select * from Department where Id not in(select distinct DepartmentId from Employee)

--查询第3条到第6条的 员工信息
-- 查询前4条 但是 不是前2条 【重点】
select top 4 * from Employee where Id not in(select top 2 Id from Employee)
--2-7条
select top 6 * from Employee where Id not in (select top 1 Id from Employee)


--======== any 、 some 关键字 表示一些、其中一个 =========
--select 字段 from 表1 where 字段 > some (select 字段 from 表2)
-- 大于 some/any 相当于大于 最小值

--查询出年龄大于任意一个女员工年龄的 男员工
select * from Employee where Sex=1 and age> any (select age from Employee where Sex=0)
select * from Employee where Sex=1 and age> some (select age from Employee where Sex=0)


--all 关键字
--大于 all 相当于 大于 最大值
--查询出年龄大于 所有女员工年龄的 男员工
select * from Employee where Sex=1 and age > all (select age from Employee where sex=0)


---=================================================
-- 次序函数 Row_Number(): 给查询结果添加一个序号列
select ROW_NUMBER() over(order by id ) as 序号, * from Department
select * , ROW_NUMBER() over(order by id) as 序号 from Department

 

 

---==================多表连接查询=================


use DBEmp

--比如:
--查询雇员的姓名和雇员所在部门名称【10单元】
select eName,dName from dept d inner join emp e on d.deptNo=e.deptNo group by dName

 

--连接查询:通过连接关键字,将两张或多张数据表 连接成一张数据表,作为数据源,放在select *from 后面

--连接查询: 分为 内连接 外连接(左外连接、右外连接、全外连接) 自连接

--内连接:关键字 inner join (inner 可以省略)
--作用: 又叫等值连接,将符合连接条件的两个表的数据查询出来
select * from dept join emp on dept.deptNo=emp.deptNo

--小结:格式:select * from 表1 join 表2 on 表1.id=表2.表1的id

 

作者还在学习中,发现错误的请在评论区留言。  如果有客友觉得文章还行的话,请点波推荐哦

www.htsjk.Com true http://www.htsjk.com/Sql_Server/43153.html NewsArticle 学习 SQL Server (4) : 多种查询语句, --======================查询========================= --select * from 表名 --简单查询---查询指定列 select 指定的列 from 表名 select id,name,sex,age from Employee --注:...
相关文章
    暂无相关文章
评论暂时关闭