没有合适的资源?快使用搜索试试~ 我知道了~
SQL语句笔记
资源推荐
资源详情
资源评论
SQL 语句笔记
第 1,2 天 SQL 简介,select 语句的使用
设有一表 Table,有 aa,bb,cc 列
Select * from Table where a=’ ’ (SQL 语句大小写不敏感,但是数据库对大小写敏感)
Select bb,cc,aa from table (可按列进行排序)
Select bb from table (选择特定的列)
Select DISTINCT bb from table (查看 BB 列中不重复的数据)
Select top 1 * from table (只返回一条数据)
Select *
From table (可换行,依个人习惯)
第 3 天表达式,条件语句,子句
select * from table where bb =’aaaaaa’ (条件语句查询)
例:5%2=1 6%2=0
有小数的数据不能取模,如实数
select cc, cc + 0.15 from table
select cc, (cc+0.15)retail from table (cc+0.15 显示出来的列名为 retail, ORACL 中适用)
select cc , cc+1 as cc1 from table (MS SQL 中一般用 AS)
select cc = produce, bb from table
select bb/2 as bbnew from table
select bb*2 as bbnew from table (如果把这些符号用在字符型字段,将会出错)
select bb MOD cc as new from table (取模:将返回一个除法结果中的商的余数部分)
select * from price where wholesale is null (Is null 表示为空)
select * from table where bb >200 (数字型字段不需要加’’)
select * from table where bb < ‘LA’ (拼音)
select * from table where bb <> ‘LA’
select * from table where bb != ‘LA’ (<> 和 != 都表示不等于)
select * from table where cc like ‘%LA%’ (LIKE 操作,注意%号,是个通配符,也可’LA%’或’%LA’)
select * from table where cc like ‘_L_A%’ (_能查找特定位置上有字符串)
select bb || ‘,’ || cc bc from table (|| 连接符号)
select bb,cc from table order by dd Desc (DESC 降序,默认是 ASC 升序, order by 2 也可)
select bb,cc from table order by bb asc,dd Desc (可同时按两列排序)
select sum(bb) from table (返回 bb 合计)
select bb,sum(dd) from table group by bb, cc (可 group by 多列,返回 bb 分组,及 dd 的合计)
select min(bb),MAX(CC) from table group by dd (对 DD 进行分组,并找出组中最大值及最小值)
select bb,sum(cc) from table group by bb , cc (可以使用在 select 中未列出的字段分组,如 cc,但在 select
出现的字段在 groupby 中不出现,将会出错)
select bb , avg(cc) from table group by bb HAVING AVG(cc) <10 ( where 中不能出现运算符,所以只能
用 Having 代替)
select * from table1,table2 (两表联合查询)
select O.bb,P.cc from table1 O,table2 P (用简单字符作为别名,以分清字段处于哪个表中)
select lastname, years*12-leavetaken as remaining from vacation where lastname like ‘B%’ and
years*12-leavetaken >50
NOT 用法 IS NOT NULL 等
Not like ‘B%’
OR (只要满足其中一个条件就返回 TRUE)
UNION ALL 与 Union 一样对表进行合并,只是不去掉重复的部分
Select * from table1 union select * from table2 (合并两表,并去掉重复记录)
INTERSECT(相交)
Select * from table1 Intersect select * from table2 ( 返回两表中都存在的记录)
MINUS(相减)返回的是在 table1 中存在而在 table2 中不存在的记录
Select * from football minus select * from softball
IN and Between (从属运算)
Select * from friends where state =’ca’ or state = ‘co’ or state=’la’
Select * from friends where state in ( ‘ca’,’co’,’la’)
Select * from table1 where cc in (22,234,12) (IN 中也可包括数字)
Select * from table where cc >0.25 and cc < 0.75 (通常用法)
Select * from table where cc between 0.25 and 0.75 ( Between 用法,返回值包括边界值)
第 4 天函 数:
Count
Select count(*) from table where bb/cc <3
Sum
Select sum(bb) from table where cc >3
AVG
Select avg(aa/bb) as new from table
Select avg(aa)/avg(bb) as new from table
Max
Select Max(name) as name from table (返回字符串值最大的,支持字符串)
Min
Select Min(name) as name from table (返回字符串排在字母表最前面的)
Select Min(aa),Max(aa) from table (可返回最小及最大数字)
Viriance (方差)
Select Viriance(aa) from table (该函数只能对数值进行操作)
日期型函数
Add_months 在日期上加上几个月
Last_day 可以返回指定月份的最后一天
Months_between 返回给定的两个日期中有多少个月
Select task,startdate,enddate,months_between(startdate,enddate) as duration from project;
Select * from project where Months_between(’19 may 95’,startdate )>0
返回在 95 年 5 月 19 日以前开始的活动
New_Time 将时间调整到指定时区
Next_day 返回与指定日期在同一个星期或之后一个星期内的,你所要求的星期天数的确切日期,如返
回你所指定的日期的星期五是几号:
Select startdate, Next_day(startdate, ‘Friday’) from project
Sysdate 返回系统的日期和时间
Select DISTINCT Sysdate from project
如果想知道今天都启动了哪些项目的话,可以输入:
Select * from project where startdate >Sysdate
数学函数
ABS 返回给定数字的绝对值
Select Abs(aa) from table
Ceil 和 Floor
CEIL 返回与指定参数相等或比给定参数大的最小整数
FLOOR 正好相反,返回与给定参数相等或比给定参数小的最大整数
COS,COSH,SIN,SINH,TAN,TANH
COS,SIN.TAN 可以返回给定参数的三角函数值,默认为弧度制
SIGN 如果参数为正数,SIGN 返回 1,负数返回-1,参数为 0SIGN 也返回 0
字符函数
CHR 返回与所给数值等当的字符
Select Code, CHR(CODE) from table
CONCAT ( || 符号是将两个字符串连接起来,CONCAT 也是)
Select CONCAT(firstname ,lastname) as “first name and last name” from name (当采用多个词作为别名,
需要加上引号,CONCAT 取得整个字段的宽度,包括空格)
INITCAP
此函数将参数的第一个字母变成大写,其他字母转换成小写
Select firstname as before, INITCAP(firstname) as after from name
Lower 和 UPPER
LOWER 将参数转换为全部小写,UPPER 相反
Lpad 和 RPAD
这两具函数需要三个参数,需要处理的字符串,及将字符串扩充的宽度,第三参数表示扩充部分用什么
字符填充
Select lastname, LPAD(lastname , 20 , ‘* ‘ ) from name
返回 lastname 及把 lastname 在字符串前面加上*号补足到 20 个字符串
LTRIM 和 RTRIM
去掉空格 select lastname , RTRIM(lastname) from name
Select lastname , RTRIM(lastname , ‘C’) from name (第二个参数默认是空格,这里则为 “C”)
REPLACE
需要三个参数,处理字串,搜索的内容,替换成的内容
Select lastname ,replace (lastname, ‘st’ ) as replacement from name
此句只有二个参数,字串中的 ST 字串将被删除
Select lastname ,replace (lastname, ‘st’,’**’ ) as replacement from name
此句三个参数,字符串中的 ST 将被*号替换
Select lastname ,replace (lastname, NULL) as replacement from name
此句只将源字串返回,不执行任何操作
SUBSTR
允许将目标字串一部分输入,三个参数,起点及长度
Select firstname, SUBSTR(firstname, 2, 3 ) from name
如果第二参数为负数,则从尾部向前至绝对值数字后往后
若第三个参数为空,则返回后面全部的
若 select * from tel 输入的 TEL 为 13390857740 ,不容易阅读,则为用下面语句加横线隔开:
Select Substr(tel ,1,3) || ‘-‘ || substr(tel ,4,8) as tel from tel
TRANSLATE
有三个参数,目标字符串,源字串,目的字符串,在目标字串及源字串中均出现的字串会被目的字串替换
Select lastname ,translate(lastname,’0123456789ABCDEFG’,’AAAAAAAAAAAAAAAAAA’) from
name
此函数对大小写敏感
INSTR
四人参数,目标字串,匹配内容,第三第四为指定搜索的起点及指出第几个满足条件的被返回
Select lastname , instr(lastname,’O’ ,2,1) from name 从字串第二个开始搜索第一个以”O”开头的字符的
位置
Length
返回指定字符串的长度
Select firstname ,length (Rtrim(firstname)) from name (用 RTRIM 去掉右边的空格)
转换函数
To_CHAR
最初功能将数字转换为字串
Select aa,To_char(aa) from table
Select aa , length (to_char(aa)) from table 将返回长度,如果 length 对数字操作将出错,所以可以判断是
不是字串的
TO_NUMBER
Select bb ,10*TO_NUMBER(bb) from table (将字串转换成数字, 可看到相乘的结果
Greatest 和 Least
返回表达式中最大和最小的
Select Greatest (‘alpha’,’bravo’,’foxtrot’,’delta’) from convert
似乎没必要使用 FROM,但如不用会出错,因 Select 和 FROM 是相对出现的,因给定表有三行,所以
返回结果也是三行,并都是 ‘foxtrot’
这两个函数也可对数字进行处理
USER
返回当前使用数据库的用户的名字
Select USER from convert
第 5 天 SQL 中的子句
Where , Starting with ,Order by ,Group by ,Having
Select * from table where cc like ‘Ca%’
等同于 select * from table where cc starting with ‘Ca’
Select location as “where waldo?” from puzzle where name=’waldo’ ( 此句返回 waldo 住在哪 )
Select * from checks order by check# (order by 是对指定的列进行排序)
Select * from checks order by check# DESC (DESC 表示按降序排序,ASC 是默认的升序,可省略)
Select * from checks order by payee asc, remark desc (可对多列进行,可用 order by 1 来代替第一列排
序)
Select payee , sum(amount),count(payee) from checks group by payee
这条语句对 payee 列进行分组并统计出每组总和,count 函数得出 payee 列每个值有多少行。
Select payee,sum(amount),count(payee) from checks group by payee, remarks
此句说明可对多列 group by ,并且只有 remarks 列也相同值时才会分为一组,否则还是单独显示
Select team,avg(salary) from orgchart group by team (返回按 team 分组后的平均工资)
无论何时,select 中出现的字段只能是在 group by 语句中出现过的,除非 select 不指定任何字段
Select team avg(salary) from orgchart where avg(salary)<38000 group by team
此条语句将出错,因为汇总函数不能工作在 where 子句
Select team avg(salary) from orgchart group by team having avg(salary)<38000 (having 返回 salary 平均
数大于 38000 的记录)
Select avg(sickleave), avg(annualleave) from orgchart group by payee having avg(sickleave)>25 and
avg(annualleave)<20 (Having 支持多条件,并可使用 select 中未指出的列)
另外 IN 操作符也可在 Having 中使用,如:
Select team, avg(salary) from orgchart group by team having team in (‘PR’,’RESEARCH’)
子句的综合运用
Select payee, sum(amount) as total,count(payee) from checks group by payee having sum(amount)>50
另:where amount>=100 group by payee having sum(amount)>50 此句也可,只是 where 会提前过滤掉
剩余16页未读,继续阅读
资源评论
sandberg0411
- 粉丝: 3
- 资源: 43
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功