🎬 博客主页:博主链接
🎥 本文由 M malloc 原创,首发于 CSDN🙉
🎄 学习专栏推荐:LeetCode刷题集!
🏅 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
📆 未来很长,值得我们全力奔赴更美好的生活✨
------------------❤️分割线❤️-------------------------
————————————————
😧关于sql_server的基础知识
😁大家好呀,今天是我第三次写sql_server,也是最近才学习sql_server,也想着记录一下自己的学习过程,并且分享给大家尼!
😧 一、sql_server技术介绍
SQL Server 是由微软公司(Microsoft)开发的关系型数(RDBMS)。RDBMS 是 SQL 以及所有现代数据库系统的基础,比如 MS SQL Server,IBM DB2,Oracle,MySQL 以及微软的 Microsoft Access。
😧二、学习前的准备工作
编程软件:SQL Server Management Studio 2012
带好你的小板凳,我们一起扬帆起航!
😧sql_server查询篇
😧三、查询的基本操作
在日常过程中,我们会遇到很多需要查询的数据信息,这个时候,我们就可以运用sql_server中的查询语句。
😧3.1基础查询
1.查询数据库中的所有列,所有行:
select * from department
select * from s_rank
select * from People
执行上述代码之后,如下图所示就是数据库中的所有数据啦!
2.查询指定列(姓名,性别,生日,月薪,电话):
select PeopleName,PeopleSex,PeopleBirth,PeopleSalary,PeoplePhone from People
如上图所示就是数据库中的查询指定列的操作啦
3.查询指定列(姓名,性别,生日,月薪,电话)(显示一个中文的列名)
select PeopleName 姓名,PeopleSex 性别,PeopleBirth 生日,PeopleSalary 月薪,PeoplePhone 电话 from People
我们只需要在对应的字段后面加一个空格,然后输入我们想命名的标题就好啦!如下如所示
4.查询出员工所在的城市(不需要重复数据显示)
select distinct(PeopleAddress) from People
在数据库中有个关键字是distinct,代表中去重的意思,当我们没带这个关键字的时候,我们显示到的数据是这样的
select PeopleAddress from People
我们会发现此时的数据中出现了两个荆州,但是我们不想要它重复出现相同的数据,那我们该怎么办呢?这时候就应该运用到***distinct***这个关键字啦!
select distinct(PeopleAddress) from People
我们此时就会发现不会重复的出现荆州了啦!
5.假设准备加工资(上调百分之二十),查询出加工资后的员工数据
select PeopleName,PeopleSex,PeopleSalary * 1.2 加薪后的工资 from People
6.假设准备加工资(上调百分之二十),查询出加工资前和加工资后的员工数据
select PeopleName,PeopleSex,PeopleSalary, PeopleSalary * 1.2 加薪后的工资 from People
😧3.2条件查询(where)
在查询的过程中,我们会只想查询我们想要知道的数据,也就类似于进行筛选的操作,在这个过程中,我们就可以不用在再很大一批的数据中进行数据的查找啦!
1.查询性别为女的员工信息:
select * from People where PeopleSex = '女'
2.查询工资大于等于10000元的员工信息:
select * from People where PeopleSalary >= 10000
3.查询出性别为女,工资大于等于10000元的员工信息
select * from People where PeopleSex = '女'
and PeopleSalary >= 10000
4.查询出月薪大于等于10000的员工或者月薪大于等于8000的女员工
select * from People where PeopleSalary >= 10000 or
(PeopleSalary >= 8000 and PeopleSex = '女')
5.查询出生年月在1980-1-1之后,而且月薪大于等于10000的女员工
select * from People where PeopleBirth >= 1980-1-1 or
(PeopleSalary >= 8000 and PeopleSex = '女')
6.查询月薪在10000-20000之间的员工信息
select * from People where PeopleSalary >= 10000 and PeopleSalary <= 20000
select * from People where PeopleSalary between 10000 and 20000
这一个查询用到了两种方法,一种是用and连接,and就相当于是要都满足两者的条件的选择,第二钟方法就是利用between and进行连接,二者选其一就行啦!
7.查询出地址在武汉或者北京的员工
select * from People where PeopleAddress = '武汉' or PeopleAddress = '北京'
select * from People where PeopleAddress in ('武汉','北京','上海')
这里也是运用了两种方法,或者就是满足其一就可以啦,还有一种就是用in(’’)里面放你想要的内容就行啦!
😧3.3排序(order by)
排序语法:
select * from 列名 order by 表名 asc
我们还看到其中还有asc,那么这个asc是啥呢?他其实是代表着升序的意思啦。那么既然有升序,是不是也还会有降序呢?没错降序就是desc,好啦我们来进入例题的讲解吧!
1.查询所有的员工信息,根据工资排序,降序
select * from People order by PeopleSalary desc
2.查询所有员工的信息,根据名字的长度排序(降序)
select * from People order by len(PeopleName) desc
3.查询出工资最高的5个人的信息
select top 5 * from People order by PeopleSalary desc
4.查询出工资最高的百分之十的员工信息
select top 10 percent * from People order by PeopleSalary desc
5.插入一个地址为空的信息
insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth,PeoPleSalary,PeoplePhone,PeopleAddtime)
values(1,1,'码云','男','1977-7-7','50000','13531511531',getdate())
6.查询出地址没有填写的员工信息
select * from People where PeopleAddress is null
7.查询出地址填写的员工信息
select * from People where PeopleAddress is not null
😧3.4运用到些许函数进行查询
1查询出80后的员工信息
有以下三种方法:
select * from People where PeopleBirth >= '1980-1-1'
and PeopleBirth <= '1989-12-31'
select * from People where PeopleBirth between '1980-1-1'
and '1989-12-31'
select * from People where year(PeopleBirth) between 1980 and 1989
2查询三十到四十岁之间,并且工资到15000-30000工资之间的员工信息
假设年龄等于当前年份-生日年份.
select * from People where
(year(getdate()) - year(PeopleBirth) >= 30 and year(getdate()) - year(PeopleBirth) <= 40)
and (PeopleSalary >= 15000 and PeopleSalary <= 30000)
3.查询出星座是巨蟹座的员工信息(6-22,7-22)
select * from People
where (month(PeopleBirth) = 6 and day(PeopleBirth) >= 22)
or (month(PeopleBirth) = 7 and day(PeopleBirth) <= 22)
4.查询出工资比关羽高的信息的信息(这里就运用到了子查询)
select * from People where PeopleSalary >
(select PeopleSalary from People where PeopleName = '关羽')
5.查询出和关羽在一个城市的的信息的信息
select * from People where PeopleAddress =
(select PeopleAddress from People where PeopleName = '关羽')
6.–查询出生肖是鼠的人员信息
–鼠 牛 虎 兔 龙 蛇 马 羊 猴 鸡 狗 猪
–4 5 6 7 8 9 10 11 0 1 2 3select * from People where year(PeopleBirth) % 12 = 4
7.查询所有员工信息,添加一列,显示生肖
select* ,
case
when year(PeopleBirth) % 12 = 4 then '鼠'
when year(PeopleBirth) % 12 = 5 then '牛'
when year(PeopleBirth) % 12 = 6 then '虎'
when year(PeopleBirth) % 12 = 7 then '兔'
when year(PeopleBirth) % 12 = 8 then '龙'
when year(PeopleBirth) % 12 = 9 then '蛇'
when year(PeopleBirth) % 12 = 10 then '马'
when year(PeopleBirth) % 12 = 11 then '羊'
when year(PeopleBirth) % 12 = 0 then '猴'
when year(PeopleBirth) % 12 = 1 then '鸡'
when year(PeopleBirth) % 12 = 2 then '狗'
when year(PeopleBirth) % 12 = 3 then '猪'
else ''
end 生肖
from People
😧如何巩固学习
把我所写的代码进行阅读,并且自行的进行题目的分析,在把题目写下来这样有利于加深自己的印象,期末遇到这些题目就不用担心啦!!文章来源:https://www.toymoban.com/news/detail-471521.html
我是爱你们的M malloc,我们下期再见呀!
文章来源地址https://www.toymoban.com/news/detail-471521.html
到了这里,关于期末sql_server复习枯燥?乏味?一文带你轻松击破sql壁垒!的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!