Sql Server查询语句


Sql Server查询语句

对于Sql Server创建的表中的数据进行查询,可以进行,基础查询,条件查询,模糊查询

基础查询

基础查询语句为:select *from 表名

select *from 表名

表示查询表中的所有数据
-- 查询指定列(姓名、生日、月薪、电话)
select PeopleName,PeopleSex,PeopleBirth,PeopleSalary,PeoplePhone from People
-- 表示从People表中查询指定的列

-- 当我们需要查询指定列,并显示别名的时候(自定义别名)
select PeopleName 姓名,PeopleSex 性别,PeopleBirth 生日,PeopleSalary 薪水,PeoplePhone 电话 from People

-- 查询出员工所在的城市(不需要重复的数据显示) 关键字 distinct
select distinct (PeopleAddress) from People

-- 假设查询出来加工资前和加工资后员工数据对比
select PeopleName 姓名, PeopleSex 性别,PeopleSalary 原始工资,PeopleSalary*1.2 加薪后的工资 from People

条件查询

对于查询的列或者表,进行条件赛选,得到我们所需的数据

SQL语句基础的运算符

-- SQL常用的运算符
= :等于表示是否相等或者赋值
!=:表示不相等
>,<,>=,<= :分别表示大于,小于,大于等于,小于等于
is null:表示为空
is not null:表示不为空
in:表示是否在其中
like:模糊查询
between...and...:比较是否在两者之间
and:逻辑与(两者必须都成立,条件才成立)
or:逻辑或(两者成立一个则条件成立)
not:逻辑非(条件成立,加not后表示表达式不成立)

-- and or not 其实相当于C语言中的&& || !
对于上述的运算符的合理运用,便能组合成条件,进行条件查询

上述运算符是基础,必须要掌握,下面是演示代码

-- 根据指定列(姓名,性别)查询女性员工
select PeopleName 姓名,PeopleSex 性别 from People where PeopleSex='女'
-- 查询月薪在10000-15000范围的男性员工
select PeopleName 姓名,PerPleSex 性别,PeopleSalary 薪水 from People where PeopleSex='男' and PeopleSalary >=10000 and PeopleSalary<=15000
-- 使用between....and...来实现上述语句
select PeopleName 姓名,PerPleSex 性别,PeopleSalary 薪水 from People where PeopleSex='男' and PeopleSalary between 10000 and 15000
-- 查询女性员工的薪水,并按照降序排序
-- 排序查询的语法,select *from 表名 order by 列(被排名的基准列)desc(默认是asc)
-- asc:升序(默认值,可以不写)desc:降序
select PeopleName 姓名,PerPleSex 性别,PeopleSalary 薪水 from People where PeopleSex='女' order by PeopleSalary desc

-- 我们对于员工的生日日期,规范使用的是“1988-1-1”(模板)
-- 三个内置函数,year(PeopleBirth),month(PeopleBirth),day(PeopleBirth),分别表示取得PeopleBirth的年月日
-- 如果我们需要查询员工是00后,并按照薪水的升序排列
select PeopleName 姓名,PeopleSex 性别,PeopleSalary 薪水,PeopleBirth 生日 from People where year(PeopleBirth) between 2000 and 2009 order by
-- 如果我们要得到员工的年龄:year(gettime())-year(PeopleBirth)
select PeopleName 姓名,PeopleBirth 生日 from People year(gettime())-year(PeopleBirth) between 30 and 40 
-- 上述查询30-40岁的员工信息
-- 查询和某一员工相同地址的所有员工信息
select *from People where PeopleAddress=(select PeopleAddress from People where PeopleName='xxx')
-- 上述语句说明,查询语句本身就是可以作为一个条件,将查询的内容,作为条件

如果我们想要得到同一属性的员工数据,可以根据年龄,使用case end语句

-- case 相当于C语言中switch
-- end是结尾关键字
-- 在case后面可以输入数据,然后在case里,使用when...then来表示C语言中的if语句,else表示为C语言中的default

-- 比如我们这个员工的表格年份是从1984年开始的,1984年是鼠
-- 鼠 牛 虎 兔 龙 蛇 马 羊 猴 鸡 狗 猪
-- 0  1  2  3  4 5 6  7  8 9 10 11

-- 如果我们想要员工属鼠的数据
select *from People where year(PeopleBirth)%12=0

-- 我们显示所有人的属象,那么就根据%以及case...end语句来判断
select *,
case (year(PeopleBirth)%12)
	when 0 then '鼠'
	when 1 then '牛'
	when 2 then '虎'
	when 3 then '兔'
	when 4 then '龙'
	when 5 then '蛇'
	when 6 then '马'
	when 7 then '羊'
	when 8 then '猴'
	when 9 then '鸡'
	when 11 then '狗'
	when 12 then '猪'
 else''
end 生肖
from People

模糊查询

模糊查询使用like关键字以及通配符来实现,下面是通配符

-- % :代表匹配任意字符(包括0)
-- _:表示匹配的字符有且只有一个
-- []: 代表匹配范围内   [1,2,3] 表示匹配数据中相关列的1 2 3数据的成员
-- [^]:表示匹配不在该范围内

下面是详细例子使用like和通配符来实现模糊查询

1.查询员工中姓王的员工
select *from People where PeopleName like '王%' --可以是王磊,王二狗等,以王头即可
2.查询电话号码,以138开头,以5181结尾
select *from People where PeoplePhone like '138%5181' and len(PeoplePhone)=13 --中间字符是任意的,且保证为13位数字
3.找到名字里面有’红‘的员工
select *from People where PeopleName like '%红%'
-- substring(PeopleName,1,1) 表示在PeopleName数据中从第一个位置,选择一个字符的数据

猜你喜欢

转载自blog.csdn.net/qq_63319459/article/details/131142534