SQL 将结果送入读者表中的借书册数字段的操作

来源:百度知道 编辑:UC知道 时间:2024/05/29 09:38:43
图书(书号,类别,出版社,作者,书名,定价);
读者(编号,姓名,单位,性别,电话);
借阅(书号,读者编号,借阅日期)

通过查询分析器用SQL命令实现在读者表中增加“借书册数’字段,统计借书者在2005年~2007年间所借书的册数,并将结果送入读者表中的借书册数字段的操作。

增加一列

alter table 读者
add column 借书册数 int

更新操作

update 读者 a
set a.借书册数 =( select count(书号)'册数'
from 借阅 b
where to_char( 借阅日期,'YYYY') between‘2005’ and ‘2007’
and a.编号 = b.读者编号
)

select r.编号,r.姓名,r.单位,r.性别,r.电话 ,n.count(*) as 借书册数
from (select 读者编号, count(*) from 借阅 where 借阅日期 betweent 2005 and 2007 group by b.读者编号 having count(*))as n ,读者 as r
where r.编号 = n.读者编号

alter table 读者
add 借书册数 int

上面的是添加字段
select count(书号)'册数'
from 借阅
group by 书号
having 借阅日期 between '2005'and'2007'