Select总结(一)

 

对关系表实施的运算

(1)选择运算

根据给定条件,从二维关系表将指定的记录挑选出来。

(2)投影运算      

把二维关系中指定的列提取出来构成一张新表

(3)连接运算

select语句:用于数据查询

一、基本查询

1、选择列-----select子句

select 列名1,列名2,。。。

from 表名

 

select 学号,姓名,性别,出生日期

from xs;

select * from xs;

 

给指定列起别名

select 列名 as 别名,列名 别名,...

from 表名

 

查询计算列

select 姓名,总学分,总学分*1.1 as 提高后的总学分

from xs

 

消除查询结果中的重复行

select distinct 列名

from 表名

 

替换查询结果中的数据

select 学号,姓名,

case

when 总学分<50 then '不及格'

when 总学分>=50 and 总学分<60 then '及格'

when 总学分>=60 then '优秀'

end

from xs;

 

使用聚合函数,通常和后面要将group by分组汇总的子句一起使用。

 

select max(总学分),min(总学分)

from xs;

max(数值型列)  求这一列的最大值

min(数值型列) 求这一列的最小值

avg(数值型列) 求这一列的平均值

sum(数值型列)求这一列的总和

count(*)求的是表格中的记录的行数

count(指定某一列)  求是指定列不为空的取值的个数(包括重复数据)

count(distinct 指定某一列) 指定列的取值去掉null值,去掉重复值后的记录的个数

 

二、选择运算------where子句:设定查询的条件。

(1)关系表达式

>  >=   <   <=   =   <> !=

select *

from xs

where 总学分<>45;

select *

from xs

where 备注<=>null;   或 where 备注 is null;

注意:空值的比较

<=>  相等关系的比较 

is null   is not null

(2)逻辑表达式

select 学号,姓名,总学分

from xs

where 总学分>=40 and 总学分<=50;

 

select 学号,姓名,总学分

from xs

where 总学分<45 or 总学分>50;

 

and   &&

or      ||

not !

 

select 学号,姓名,总学分

from xs

where not (总学分>=40 and 总学分<=50)

 

select 学号,姓名,总学分

from xs

where !(学号='081101');

(3)between  val1  and   val2 :在[val1,val2]  连续的区间

select 学号,姓名,总学分

from xs

where 总学分 between 45 and 50;

 

select 姓名,出生时间

from xs

where 出生时间 not between '1989-1-1' and '1989-12-31';

 

 

select 姓名,出生时间

from xs

where 出生时间>='1989-1-1' and 出生时间<='1989-12-31';

(4)不连续的若干个取值:in (不连续的若干个取值)

select 姓名,总学分

from xs

where 总学分=38 or 总学分=40 or 总学分=48;

等价

select 姓名,总学分

from xs

where 总学分 in (38,40,48);

 

select 姓名,专业名,总学分

from xs

where 专业名='计算机' or 专业名='通讯工程';

 

select 姓名,专业名,总学分

from xs

where 专业名 in('计算机','通讯工程');

 

(5)模糊查询 like '模式匹配字符串'

在“模式匹配字符串”中可以:%任意个任意的字符   _一个任意的字符

select 学号,姓名,总学分

from xs

where 姓名 like '王%';

王林   王小小   王

 

select 学号,姓名,总学分

from xs

where 姓名 like '王_';

 

select 学号,姓名

from

 xs

where 学号 like '__11__';

 

select *

from xs

where 姓名 like 'a%';

 

select *

from xs

where 姓名 like '%tf';

 

select 姓名,专业名

from xs

where 姓名 like '%#_%' escape '#';

 

正则表达式

 

rlike '正则表达式的字符串'

或者 REGEXP '正则表达式的字符串'

 

select 学号,姓名,专业名

from xs

where 姓名 rlike '^王';

 

select 学号,姓名,专业名

from xs

where 姓名 rlike '林$';

 

rlike '.*'

rlike '^08.*08$'

 

rlike 'a+'

rlike '[^a-z]'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值