Oracle SQLPlus:Select语句,其中(?)和order by

时间:2014-12-09 23:29:19

标签: sql oracle sqlplus pico

我正在写一篇大量的论文,作为编码表和插入值的一部分,我得到了一些我必须回答的问题;但是其中一个我遇到了很多麻烦,所以我希望你们其中一个人可以帮助我!问题如下:

  

显示当前已签出的book_copies,已于2014年10月10日过期。输出将显示bookid,copy_num,客户姓氏和名字,逾期天数(此列没有负数或零!)。按bookid排序,copy_num。请不要在这个问题中更新balance_due。

Book_copy表:

create table BOOK_COPY
(Copy_Num number(5),
Bookid  number(5),
out_to_CID number(6),
Date_Out date,
Date_Due date,
Held_Until date,
held_for_CID number(6),
CONSTRAINT PKCOPY PRIMARY KEY (Copy_Num,Bookid),
CONSTRAINT FKCID FOREIGN KEY (out_to_CID) references CUSTOMER (CID),
CONSTRAINT FKHeld FOREIGN KEY (held_for_CID) references CUSTOMER (CID),
CONSTRAINT FKBookid FOREIGN KEY (Bookid) references BOOK (Bookid));

客户表:

create table CUSTOMER
(CID number(6),
First_Name varchar2(12),
Last_Name varchar2(12),
Category varchar2(7),
Balance_Due number(4,2),
SponsorID number(6),
CONSTRAINT pkCID PRIMARY KEY (CID),
CONSTRAINT fkCUSTOMER foreign key (sponsorID) references customer);

2 个答案:

答案 0 :(得分:1)

如果您有兴趣了解从10月10日过期的天数,您可以使用这段代码:

select
bookid,
copy_num,
Last_Name,
First_Name,
Date_Due - to_date( '10-OCT-2014') total_days_overdue
from
book_copy bc
inner join customer c on bc.out_to_cid = c.cid
where
Date_Due >'10-OCT-2014'
Order by
bookid, copy_num;

答案 1 :(得分:0)

内部联接,从另一个日期减去一个日期将如下所示:

select
  bookid, 
  copy_num, 
  Last_Name, 
  First_Name, 
  Date_Due - sysdate total_days_overdue
from
  book_copy bc
  inner join customer c on bc.out_to_cid = c.cid
where
  Date_Due > to_date('20141010','yyyymmdd')
Order by 
  bookid, copy_num