Oracle基本语句
Oracle数据库基本语句:
假设有表job,有4个字段JOB,SKILL,LS,ID都为varchar类型
添加一行数据
insert into job (JOB,SKILL,LS,ID) values ('行长','领导','1’,'1'); //为job表添加一条数据,其中job为‘行长’,skill为‘领导’,ls为’1‘,id为’1‘
删除一行数据
delete from job where ID = '1'; //删除job表中id为’1‘的所有数据
去重查询
select distinct name from job //查询表job中name字段的不重复数据
假设有表job,有4个字段JOB,SKILL,LS,ID都为varchar类型
添加一行数据
insert into job (JOB,SKILL,LS,ID) values ('行长','领导','1’,'1'); //为job表添加一条数据,其中job为‘行长’,skill为‘领导’,ls为’1‘,id为’1‘
删除一行数据
delete from job where ID = '1'; //删除job表中id为’1‘的所有数据
去重查询
select distinct name from job //查询表job中name字段的不重复数据
修改一行数据
update job set JOB=’' where ID=''; //将job表中id为''数据的job字段更新为''
模糊查询
select * from job where job like '%航空%' //查询job表中job字段中含‘航空’的所有数据
修改表名
alter table job rename to work; //将表job改名为work
修改表中元素名
alter table job rename column skill to sky; //将job表中的skill字段改为sky
添加一个字段
alter table job add (c varchar2(50)) //为job表添加一个varchar类型,长度为50,名称为c的字段
删除一个字段
alter table job drop(c) //删除job表中名称为c的字段
联合查询(其中nine_ans,nine_score为表名,type,score,objid,num,ans为字段名)
select b.type, sum(b.score) as score
from nine_ans t,nine_score b
where t.objid='402881613a49410e013a4a8e7b89001e'
and t.num=b.num
and t.ans = b.seclection
group by type)
行转列 (使用pivot函数)
select * from (select b.type, sum(b.score) as score
from nine_ans t, nine_score b
where t.objid = '402881613a49410e013a4a8e7b89001e'
and t.num = b.num
and t.ans = b.seclection
group bytype)pivot(sum(score)for type in ('H' as H,'E'
as E))
用于替换字段值docode函数
select decode(需要替换的字段名,需要替换的字段值,替换后的字段值,返回字段名)as 更换字段名可以不更换
select userid,name,sex,edu,tel,email,(select j.job from job j whereid = u.job) from userinfo u where usergroup in (select usergroupfrom admin where username like 'dm%')
update job set JOB=’' where ID=''; //将job表中id为''数据的job字段更新为''
模糊查询
select * from job where job like '%航空%' //查询job表中job字段中含‘航空’的所有数据
修改表名
alter table job rename to work; //将表job改名为work
修改表中元素名
alter table job rename column skill to sky; //将job表中的skill字段改为sky
添加一个字段
alter table job add (c varchar2(50)) //为job表添加一个varchar类型,长度为50,名称为c的字段
删除一个字段
alter table job drop(c) //删除job表中名称为c的字段
联合查询(其中nine_ans,nine_score为表名,type,score,objid,num,ans为字段名)
select b.type, sum(b.score) as score
from nine_ans t,nine_score b
where t.objid='402881613a49410e013a4a8e
and t.num=b.num
and t.ans = b.seclection
group by type)
行转列 (使用pivot函数)
select * from (select b.type, sum(b.score) as score
where t.objid = '402881613a49410e013a4a8e
and
and t.ans = b.seclection
group
用于替换字段值docode函数
select decode(需要替换的字段名,需要替换的字段值,替换后的字段值,返回字段名)
select userid,name,sex,edu,tel,email,(select j.job from job j whereid = u.job) from userinfo u where usergroup in (select usergroupfrom admin where username like 'dm%')