SQL Server—T-sql使用Select查询详解
SQL Server—T-sql使用Select查询详解
在SQLserver数据库中常用的查询方式有多表查询,条件查询,嵌套查询,分页查询,以及常用到的各种查询语句等等...
今天小编就在此逐个分享一下!!!
-
select 查询 (扩建)
-
一 基础查询(扩展)
-
--1 查询所有的数据信息
-
select * from Hero
-
--2 查询部分行
-
select Name,Age from Hero
-
--3 and
-
-- 查询并设置查询条件 使用and 连接俩个条件的
-
select Sex from hero where Age>=10 and Name='鲁班七号'
-
--4 as和=
-
查询的时候可以查出来数据设置别名 使用as和=, select Skill as 技能,name,Birthday as 出生年月 from Hero where Sex='男' or Age = 20
-
--如果使用等号查询出来的列名修改名称时候 需要把别名放在=号前面
-
select 姓名= Name from Hero where id = 1
-
--5 加号的使用
-
-- 如果加号使用在字符之间都是拼接的意思
-
-- Skill+'[' + Name+ ']' 切换刀[花木兰] select 技能和姓名= Skill+'[' + Name+ ']' from Hero where Id = 4
-
--如果加号使用在俩个数字之间就是加法运算意思
-
select id加age = Id + Age from Hero where Id = 2
-
--6 排序
-
-- Age加10之后进行升序或者降序进行排列
-
--语法: order by 根据哪个字段进行排序
-
--DESC 降序
-
-- ASC 升序
-
select Name,NewAge=Age+10 from Hero order by Age ASC
-
--7 模糊查询
-
--使用like 使用like时候,字段中值不一定与查询内容完全匹配 只要字段是包含查询内容的即可
-
-- 如何查询以狄开头的数据 查询人在中间的一条记录
-
--% 代表可以匹配的字符串长度大于等于0
-
select * from Hero where Name like '狄%'
-
-- _ 一个下划线就是一个字符
-
select * from Hero where Name like '_狄%'
-
--配合 not , not like 查询除了哪些剩余的数据
-
select * from Hero where name not like '花%'
-
-
-
-
-
-- 8 查询空行
-
select * from XueSheng where Card is null
-
-
-
-
-
-- 9 top 限制查询出来的行数
-
-- 分页查询 -
-
- top 2 查询前两条 姓名 年龄 性别
-
select top 2 Name ,Age,Sex from XueSheng
-
-- 10 多列查询
-
-- 排列的时候当第一条条件一样的情况下,对比第二个条件
-
select Name,C#,Python from XueSheng where C# > 60 order by C# Desc, Python Desc
-
-- 11 范围查询
-
-- between and 更像一个范围查询
-
select Name,Age from XueSheng where Age between 20 and 90
-
-- 12 in 使用特点 更像一个枚举类型
-
select Name from XueSheng where Name in('铁拐李')
-
-- 13 查询当前时间
-
select GETDATE() as 时间
-
-- 14 别名查询
-
select DTime,StudentId,a.CarNo,StudentName,Gender,ClassName from Students s inner join StudentClass c on c.ClassId = s.ClassId inner join Attendance a on a.CarNo = s.CardNo where Age between '10' and '20'
-
-
二 多表查询
-
1 内连接查询 inner join
-
-- 内连接查询: 求俩表之间的公部分,类似于交集
-
-- inner join 内连接语法: inner join 表1,字段= 表2,字段
-
-- 语法: select 列名 from 另一张表名 inner join 表名 on 表1,字段=表2,字段 where条件
-
select ChengJiTable.StudentId,StudentName,Age,Score Address from ChengJiTable inner join XueSheng on XueSheng.StudentId = ChengJiTable.StudentId where Score >= 600
-
-- 多表内连接
-
select XueSheng.StudentId,StudentName,Age,Score,ClassName from ChengJiTable inner join XueSheng on XueSheng.StudentId = ChengJiTable.StudentId inner join BanJiTable on BanJiTable.ClassId = XueSheng.ClassId
-
-
2 左外连接查询 left outer join
-
--左外连接总结
-
--1 查询结果包含俩个表所有满足条件的记录
-
--2 查询结果包含 不满足左表的数据设置为null
-
-- 左外连接查询 left outer
-
-- 查询的数据需要满足一下2点
-
-- 1 两个表的所有满足的条件 也就是满足StudentId相等的
-
-- 2 不满足左表的记录 设置null
-
select XueSheng.StudentId,studentName,Age,Score from ChengJiTable left outer join XueSheng on ChengJiTable.StudentId = XueSheng.StudentId
-
-- 交换表之间的顺序 左表变了
-
-- 1 两个表的所有满足的条件
-
select XueSheng.StudentId,StudentName,Age,Score from XueSheng left outer join ChengJiTable on ChengJiTable.StudentId = xuesheng .StudentId
-
-
3 右外连接查询 right outer join
-
--右外连接总结
-
-- 1 查询结果包含俩个表所有满足条件的记录
-
-- 2 查询结果包含 不满足右表的数据设置为null
-
--右外连接(right outer join ...on)
-
--1查询结果包含俩个表所有满足条件的记录 例如 1、2 、3
-
--2 包含在学生表不满足右表BanJiTable4的设置为null
-
select XueSheng.StudentId,StudentName,Age,ClassName from XueSheng left outer join BanJiTable on XueSheng.ClassId = BanJiTable.ClassId
-
-
-
三 分组查询(条件查询)
-
-- 条件查询语句作用:
-
-- 1. where: 从数据源中去除不符合搜索条件的数据
-- 2. group by: 根据一定的规则将一个数据集划分成若干个小区域,然后针对每个区域进行数据处理。即分组查询,一般是和聚合函数配合使用。
-- 3. having: 在分组中 去除不符合的数据
-- 4. order by 元素 ASC: 升序
-- 5. order by 元素 DESC: 降序
-- between and 是一个范围
-- in 在....
-- 李% 以李开头
-- _李_% 中间是李
where、having、group by、order by的顺序是:where -> group by -> having -> order by (where先执行,再groupby分组;groupby先分组,having在执行。)
-
查询重复的记录
-
select Sex,COUNT(Sex) as 个数 from XueSheng group by Sex having Count(Sex) >= 2
-
-
四 子查询(嵌套查询)
-
select cid from first where cid = (select cid from second where cid =1)
-
-
五 分页查询
-
-- 从第2条数据开始查询,查询4条数据。
-
select * from Student order by id offset 2 rows fetch next 4 rows only
-
-