加入收藏 | 设为首页 | 会员中心 | 我要投稿 广州站长网 (https://www.020zz.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle数据库常用语法

发布时间:2020-12-26 17:38:29 所属栏目:百科 来源:网络整理
导读:副标题#e# 基本 --新建表: create table table1( id varchar(300) primary key,name varchar(200) not null); --插入数据 insert into table1 (id,name) values (‘aa‘,‘bb‘); --更新数据 update table1 set id = ‘bb‘ where id=‘cc‘; --删除数据 d

insert into reader(cardid,address)
values (‘uuu‘,‘luna‘,address)
values (‘vvv‘,‘B‘,‘harry‘,address)
values (‘www‘,‘C‘,‘chander‘,‘2‘,‘professor‘,address)
values (‘yyy‘,‘joey‘,address)
values (‘zzz‘,‘richard‘,address)
values (‘OOO‘,‘micheal‘,address)
values (‘ppp‘,‘richal‘,address)
values (‘abp‘,‘michal‘,address)
values (‘ccp‘,‘mike‘,‘bupt‘);
--插入数据-borrow
insert into borrow(cardid,bookid,borrowdate) values(‘xxx‘,‘aaa‘,‘2014-4-29‘);
insert into borrow(cardid,‘bbb‘,‘ccc‘,‘2014-4-28‘);
insert into borrow(cardid,borrowdate) values(‘yyy‘,‘ddd‘,‘2014-4-27‘);
insert into borrow(cardid,borrowdate) values(‘zzz‘,borrowdate) values(‘uuu‘,‘2014-4-26‘);
insert into borrow(cardid,borrowdate) values(‘vvv‘,borrowdate) values(‘www‘,‘2014-4-26‘);
表信息如下:

book------> reader-------> borrow

?

?

?

3. 查询A单位借阅图书的读者人数和人员详细信息

人数:

with vt1 as
(select cardid from reader where reader.org=‘A‘)
select count(1) from vt1 where exists (select cardid from borrow where borrow.cardid=vt1.cardid);


详细信息:

with vt1 as
(select cardid,org from reader where reader.org=‘A‘)
select cardid,org from vt1 where exists (select cardid from borrow where borrow.cardid=vt1.cardid);


4.查询借书证号尾字符为‘p‘的读者

select cardid,org from reader where cardid like ‘%p‘;


5.?查询名字以m开头的女性读者,‘1’显示为女,‘2’显示为男

select cardid,
case when gender=‘1‘ then ‘女‘ when gender=‘2‘ then ‘男‘ else ‘其他‘ end gender
from reader where name like ‘m%‘;


6.?2014年2-4月借过书的读者

1)查询满足条件的读者(仅包含cardid)--未去重

  方式一:

select cardid,borrowdate from borrow where to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy‘)=‘2014‘
and to_char(to_date(borrowdate,‘mm‘)>=‘02‘
and to_char(to_date(borrowdate,‘mm‘)<=‘04‘;
方式二:

select cardid,‘yyyy‘)=‘2014‘ --查询
and to_char(to_date(borrowdate,‘yyyy-mm‘)>=‘2014-02‘
and to_char(to_date(borrowdate,‘yyyy-mm‘)<=‘2014-04‘;
方式三:

select cardid,‘yyyy-mm-dd hh24:mi:ss‘);


2) 查询+去重

select distinct cardid from borrow where to_char(to_date(borrowdate,‘yyyy‘)=‘2014‘ --查询+去重
and to_char(to_date(borrowdate,‘yyyy-mm‘)<=‘2014-04‘;
select distinct cardid from borrow where to_date(borrowdate,‘yyyy-mm-dd hh24:mi:ss‘);


3)查询+去重+读者姓名等信息

with vt1 as (select distinct cardid from borrow where to_char(to_date(borrowdate,‘yyyy‘)=‘2014‘ and to_char(to_date(borrowdate,‘yyyy-mm‘)>=‘2014-02‘and to_char(to_date(borrowdate,‘yyyy-mm‘)<=‘2014-04‘)select cardid,org from reader where exists (select cardid from vt1 where vt1.cardid=reader.cardid);

(编辑:广州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读