Alter Table 读者 Add 借书册数 int
update 读者
set 借书册数=b.借书册数
from 读者 a join (select 读者编号, count(*) as 借书册数 from 借阅 where 借阅日期 between '2005-01-01' and '2007-12-31' group by 读者编号) b on a.读者编号=b.读者编号
[Quote=引用楼主 qq363536704 的回复:]
SQL 将结果送入读者表中的借书册数字段的操作
图书(书号,类别,出版社,作者,书名,定价);
读者(编号,姓名,单位,性别,电话);
借阅(书号,读者编号,借阅日期)
通过查询分析器用SQL命令实现在读者表中增加“借书册数’字段,统计借书者在2005年~2007年间所借书的册数,并将结果送入读者表中的借书册数字段的操作。
[/Quote]
alter table 读者 add 借书册数 int null
go
update 读者 set 借书册数 = (select count(1) from 借阅 where 读者编号 = 读者.编号 and year(借阅日期) between 2005 and 2007) from 读者
update 读者 set 借书册数 = (select count(1) from 借阅 where 读者编号 = 读者.编号 and year(借阅日期) between 2005 and 2007) from 读者 where 编号 in (select 读者编号 from 借阅 )