SQL server查询语句

基础查询语句

--1 查询表中所有信息 其中*代表所有列
select * from people

--2 查询people表中某些列
select PeopleName,PeopleSex,PeopleBirth,PeopleSalary,PeoplePhone from people

--3 查询people表中某些列 以指定名字显示出来
select PeopleName as 姓名,PeopleSex as 性别,PeopleBirth as 出生日期,PeopleSalary as 薪水,PeoplePhone as 电话号码 from people
--不用as 也是可以的
select PeopleName 姓名,PeopleSex 性别,PeopleBirth 出生日期,PeopleSalary 薪水,PeoplePhone 电话号码 from people

--4 查询所有员工城市分布,且不想要重复的数据显示
--使用distinct 关键字去约定不显示重复城市
select distinct(PeopleAddress) from people

--5 假设 准备加工资
--涨薪20% 查看加工资后的员工数据
select PeopleName,PeopleSex,PeopleSalary=PeopleSalary*1.2 from people 

条件查询

--1 条件查询	
--查询性别为女的员工信息
select * from people where PeopleSex='女'

--2 查询工资大于等于50000的所有员工信息
select * from people where PeopleSalary>=50000 

--3 当有多个条件使用and将条件联合起来
--查询性别为女 且 月薪大于50000的人的信息
select * from people where PeopleSex='女' and PeopleSalary>=50000

--4 当查询条件复杂可以用括号括起来
--查询月薪大于50000,或者月薪大于20000的女生
--select * from People where 条件1 or 条件2
select * from People where PeopleSalary>=50000 or (PeopleSex='女' and PeopleSalary>20000)

--5 查询薪资在20000到50000之间的人员信息
select * from people where PeopleSalary>=20000 and PeopleSalary<=50000
--像这种范围,也可以使用between....and...去查询,效果是一样的
select * from People where PeopleSalary between 20000 and 50000

查询空内容

--1 查询地址没有填写的员工
--查询字段内容为空的时候,不可以使用= 去判断内容是否为空
--有专门的使用的是is
select * from people where PeopleAddress is NULL 

--查询地址不为空的员工信息
select * from people where PeopleAddress is not NULL

--NULL和空字符串是不一样的,在插入的时候直接不插入该列就是空值,如果插入的时候写了该列,插入数据给的是''
--这相当于给的是一个空字符串,所以在查询的时候,查询空字符串应该使用等于,如下所示
select * from poeple where PeopleAddress =''

子查询

--1 查询出 比小刘工资高的员工的信息
--先查询出小刘那一行的工资这一列,要确保比对的数据只有一个,要不然是不被允许的
--当报错说子查询的值不只一个的时候,就要检查约束的条件是不是不够多
--使用>去比较的时候 保证子查询出来的结果是一个可比较的值
select * from people where PeopleSalary >
(select PeopleSalary from people where PeopleName='小刘' and RankID=1)

--2 查询和小陈一个城市的人的信息
select * from people where PeopleAddress =
(select PeopleAddress from people where PeopleName='小陈')

模糊查询

--模糊查询
--使用like关键字和一些通配符去查询

--% 代表匹配0个字符,1个字符,或者多个字符
-- ——代表匹配的只有一个字符
--[]代表匹配范围内
--[^]代表不在范围内

--1 查询叫 某某刘的员工信息
select * from people where PeopleName like '%刘'

--2 查询people中名字中含有白的 但是不知道白在名字的哪个字
select * from people where PeopleName like '%白%'

--3 查询出名字中含有’白‘或者’拜‘的员工信息
select * from people where PeopleName like '%白%' or PeopleName like '%拜%'

--4 查询出名字是刘的员工 且名字是两个字,姓啥咱不管
select * from people where PeopleName like '_刘' 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值