本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TOC
前言
提示:以下是本篇文章正文内容,下面案例可供参考
1、别名
1.1字段名后面 使用空格 1.2 字段名后面使用as连接
2、过滤重复行
代码如下(示例):
select distinct 字段名 from 表名
3、逻辑运算符的优先级
代码如下(示例):
优先级是NOT>AND>OR
4、或者和并且 or和and关键字连接两个条件
5、信息排序
关键字:order by 代码如下(示例):
语法: select * from 表名order by 排序的字段名
desc(降序)/asc(升序 默认 写不写都可以)
6、多列排序
排序原则是依照排序列从左至右的次序确定优先级别
7、返回指定的行数
关键字:top n (n是返回的行数) 代码如下(示例):
select top * from 表名order by 排序的字段名 desc
8、返回指定内容的百分比
代码如下(示例):
select top 百分比数值 percent * from 表名 order by 字段名desc
9、通配符
关键字 like连接
1.1 “%”通配符匹配0至多个任意字符
1.2 “_”通配符仅匹配任意一个字符,如需匹配两个字符,则使用“_ _”。
1.3 "[]"通配符匹配一个集合
10、 IN运算符定义
用于判断一个值是否在一个指定的数据集合之内,也称为“成员条件运算符”
11、between and
代码如下(示例):
select * from 表名where 字段名 between 取值 and 取值
12、in运算符的语法
代码如下(示例):
select * from 表名where 字段名 in('条件','条件')
13、作业练习
use SuperKTV
--别名
select singerid 歌手编号,singername 歌手姓名,singertype 歌手类型 from singer
select singername as 歌手姓名,singertype as 歌手类型 from singer
insert into singer values('华晨宇','男歌手','2000-9-9','安徽')
select * from singer
select * from type
--过滤重复的信息
select distinct area 地区 from singer
--算术运算符的讲解
create table op_op2(num int)
insert into op_op2 values (100)
select * from op_op2
if object_id('tempdb..op_op1')is not null
begin
drop table op_op1
end
select num 原数字,num+50 加法运算,num-60 减法运算,num * 2 乘法运算,num/5 除法运算 from op_op2
use HUAWEI
select * from rank
select * from Employee
---题目一:查询编号为5的所有员工的信息
select name 姓名 ,address 住址, salary 工资 from employee where Employeeid=5
---题目二:查询工资大于15000的员工信息
select * from employee where Salary>15000
---题目三:查询住址不在深圳的员工的信息
--非等于的第一种写法
select * from employee where address !='深圳'
--非等于的第二种写法
select * from employee where address <>'深圳'
---多条件的查询----------------
--题目五:查询工资在2w以上并且住址实在深圳的员工的信息、
select * from Employee where salary>20000 and Address='深圳'
--题目六:查询员工性别是女的或者住址在深圳的员工
select * from Employee where gender='女' or address='深圳'
--题目七:查询员工信息 住址在武汉或者在南京但是工资都必须大于10000
select * from Employee where (Address='南京' or Address='武汉') and salary>10000
--题目八:查询员工的工资 由高到低排序
select * from Employee order by salary asc
--题目九:根据员工的rankID和工资排序
select * from employee order by Salary ,RankID
--题目十:返回指定行数 返回工资最高的前三位员工的信息
select top 3 * from Employee order by salary desc
--题目十一:返回行数的百分比
select top 100 percent * from Employee order by salary desc
--题目十二:匹配姓为李的员工的信息 %
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 Address='南京' or Address='武汉'
select * from Employee where Address in('武汉','南京')
--题目十七:查询员工的工资大于15000但是小于20000的信息
select * from Employee where salary between 10000 and 20000