我想选择租用了汽车的最新客户;在参数中输入了id。
这是我的代码:
create or replace procedure Q9(idcar int) as
cursor c is
select client
from rent r
where r.car=idcar
and begindate = (select max(begindate)
from rent r2
where r.client=r2.client);
clientn varchar(50);
begin
for k in c loop
select clientname
into clientn
from client c
where c.idcl=k.client;
dbms_output.put_line(clientn);
end loop;
end;
答案 0 :(得分:3)
这是您的查询:
select client
from rent r
where r.car=idcar and
begindate = (select max(begindate) from rent r2 where r.client=r2.client);
由于相关的子查询,这得到了每个客户端的最新记录。
以下所有租金均为begindate
的最大值:
select r.client
from rent r
where r.car = idcar and
r.begindate = (select max(r2.begindate) from rent r2);
以上可能没有任何回报。您可能需要:
select r.client
from rent r
where r.car = idcar and
r.begindate = (select max(r2.begindate) from rent r2 where r2.car = r.car);
这将返回在最近租用汽车的日期租用汽车的客户。
编辑:
以上应该有效。如果您只想要一个值,则可以执行以下操作:
select client
from (select r.client
from rent r
where r.car = idcar
order by begindate desc
) t
where rownum = 1;