select 语法+别名的使用+连接运算符+distinct+where+like+转义字符+between+in

来源:互联网 发布:java入门培训费用 编辑:程序博客网 时间:2024/06/06 00:26

select 语法
select 【distinct | all 】{* | 表达式 | 【列名,列名,… 】}
from {表名| 子查询 } 【别名】
【where condition】
【connect by condition 【start with condition】】
【group by expression【,….】
【havaing condition【,….】
【{union | intersect | minus }】
【order by expression asc|desc【,expression asc|desc….】】
【for update 【of 【schema】tableName|view】column】
【nowait】

all:查询的结果不管是重复值,都显示,默认是all
distinct:查询的结果有重复值,只显示一个
*:显示表和视图的全部列
connect by condition :查询是搜索的一个连接描述

start with condition:搜索的开始条件

group by expression:分组查询,后面可为列名或者表达式,表示对搜索的结果进行分组
order by expression :排序操作
for update 【of 【schema】tableName|view】column 】:在查询的时候对表进行锁定,不允许其他用户对表进行操作,直到解锁为止.

havaing condition:分组结果后的筛选条件
union | intersect | minus :分别实现集合的交集,并级,差级的操作

注意:havaing 针对的是group by

别名的使用
1.select age 年龄,name 名称 from
2.select age as 年龄,name as 名称 from
3.select * from student s

连接运算符||
将查询结果连接在一起
这里写图片描述

distinct
去掉查询中重复的值
显示不重复的数据,重复的数据就显示一个,在列前面使用。如果不指定distinct,默认all关键字。
distinct 后面跟很多列,这比较重复的值,是比较每一行相应的列是否都相同,如果都相同,就显示一行.

这里写图片描述

create table student2(
sno varchar2(10) primary key,
sname varchar2(20),
sage number(2),
cno varchar2(2)
);
insert into student2 values(‘1’,’lili’,18,’1’);
insert into student2 values(‘2’,’lili’,18,’1’);
insert into student2 values(‘3’,’lili’,20,’1’);
insert into student2 values(‘4’,’lili’,21,’2’);

select distinct sname,sage,cno from student2

这里写图片描述

where
过滤查询结果
比较操作符
>,<,>=,<=,=,<>或!=
between and ,or
any,all
in,not in
like
is not null,is null

like
通配符 %,_
%:0个或者多个字符
_:一个字符

这里写图片描述

转义字符
可以使用escape关键字来定义转义字符]
列如:’%\%%’ escape ‘\’;

这里写图片描述

create table student2(
sno varchar2(10) primary key,
sname varchar2(20),
sage number(2),
cno varchar2(2)
);
insert into student2 values(‘1’,’lilia’,18,’1’);
insert into student2 values(‘2’,’lilib’,18,’1’);
insert into student2 values(‘3’,’lilic’,20,’1’);
insert into student2 values(‘4’,’lilid’,21,’2’);

select sname,sage,cno from student2
where sname like ‘%lili%’

这里写图片描述
between

获取条件在一个区间所对应的数据
create table student2(
sno varchar2(10) primary key,
sname varchar2(20),
sage number(2),
cno varchar2(2)
);
insert into student2 values(‘1’,’lili’,18,’1’);
insert into student2 values(‘2’,’lili’,18,’1’);
insert into student2 values(‘3’,’lili’,20,’1’);
insert into student2 values(‘4’,’lili’,21,’2’);

select sname,sage,cno from student2
where sage between 1 and 30
这里写图片描述

in
获取条件在一个集合所对应的数据
create table student2(
sno varchar2(10) primary key,
sname varchar2(20),
sage number(2),
cno varchar2(2)
);
insert into student2 values(‘1’,’lili’,18,’1’);
insert into student2 values(‘2’,’lili’,18,’1’);
insert into student2 values(‘3’,’lili’,20,’1’);
insert into student2 values(‘4’,’lili’,21,’2’);

select sname,sage,cno from student2
where sage in (18,20)

这里写图片描述

0 0
原创粉丝点击