首页 > 试题广场 >

根据下面给的表和 SQL 语句,问执行 SQL 语句更新多少

[单选题]
根据下面给的表和 SQL 语句,问执行 SQL 语句更新多少条数据?sql 语句:
update Books set NumberOfCopies = NumberOfCopies + 1 where AuthorID in select AuthorID from Books group by AuthorID having sum(NumberOfCopies) <= 8
表中数据:
BookID Tittle Category NumberOfCopies AuthorID
1 SQL Server 2008 MS 3 1
2 SharePoint 2007 MS 2 2
3 SharePoint 2010 MS 4 2
5 DB2 IBM 10 3
7 SQL Server 2012 MS 6 1
  • 1
  • 2
  • 3
  • 4
  • 5
根据sql执行规则,先执行  select AuthorID from Books group by AuthorID having sum(NumberOfCopies) <= 8 
表示按照AtuhorID进行分组,分组之后  sum(NumberOfCopies)统计数量和,而且要求 sum(NumberOfCopies) <= 8 和小于等于8 有上面的数据可知只有AuthorID 为2 的符合条件,所以更新只有2条。

 select AuthorID,  sum(NumberOfCopies)  from Books group by AuthorID   这条语句执行的结果如下样子

AtuhorID   sum(NumberOfCopies) 
1                 9
2                 6
3                 10
发表于 2015-08-04 08:59:07 回复(1)
更多回答
  1. update Books 
  2.   set NumberOfCopies = NumberOfCopies + 1 
  3.   where AuthorID in 
  4.     select AuthorID 
  5.       from Books 
  6.       group by AuthorID 
  7.       having sum(NumberOfCopies) <= 8 
4-7行为子查询,首先将数据按AuthorID 分组,然后分组之后按 sum(NumberOfCopies) <= 8 过滤,最后将取出过滤结果中的AuthorID 
那么:分组结果为:
1 SQL Server 2008 MS 3 1 
7 SQL Server 2012 MS 6 1

2 SharePoint 2007 MS 2 2 
3 SharePoint 2010 MS 4 2 

5 DB2 IBM 10 3 
过滤结果为:
2 SharePoint 2007 MS 2 2 
3 SharePoint 2010 MS 4 2 
子查询的结果为:
2
综上,可以将原sql语句改写成如下内容:
  1. update Books 
  2.   set NumberOfCopies = NumberOfCopies + 1 
  3.   where AuthorID in (2)
所以,更新结果为:
2 SharePoint 2007 MS 3 2 
3 SharePoint 2010 MS 5 2 




发表于 2018-03-05 18:49:51 回复(1)
group by AuthorID 将数据分为3组
having sum(NumberOfCopies) <= 8 只有一组,就是SharePoint的组
所以执行两次
发表于 2015-08-30 10:07:05 回复(0)
update Books set NumberOfCopies = NumberOfCopies + 1 where AuthorID in 
    select AuthorID from Books group by AuthorID having sum(NumberOfCopies) <= 8

先执行后面的子查询,得到的AuthorID为2,(一共有两条AuthorID为2的记录),上面的语言等价于下面的语句。所以一共更新两条记录
update Books set NumberOfCopies = NumberOfCopies + 1 where AuthorID in (2)

编辑于 2015-07-29 09:13:49 回复(0)
我看答案是2,重新看了一下题目,觉得2也是有道理的,update Books set NumberOfCopies = NumberOfCopies + 1 where AuthorID in select AuthorID from Books group by AuthorID having sum(NumberOfCopies) <= 8 ,这里的是books group,是不是得把相同数据库的NumberOfCopies 加起来,再与8比较,如 SQL的BookID有1和7,加起来 NumberOfCopies 是9, SharePoint 加起来是6,DB2是10,所以只更新第2,3条
发表于 2015-04-03 11:03:31 回复(0)

having可以查出来有两条

发表于 2019-12-17 09:47:48 回复(0)
分组后:
AtuhorID   sum(NumberOfCopies) 
1                          9
2                          6
3                         10
只有“2”组,符合条件,所以更新了BOOKID的“2,3”。

发表于 2019-06-27 21:48:17 回复(0)
  1. 主要是group by AuthorID,这里没看到的话就会选成单个NumberOfCopies <= 8 的组得到4.
  2. AuthorID相同的分成了3组。
  3. 所以:
AtuhorID   sum(NumberOfCopies)
1                          9
2                          6
3                         10
    得到只需要修改1、2,两个记录。
编辑于 2019-06-22 11:51:06 回复(0)
AtuhorID   sum(NumberOfCopies) 
1                 9
2                 6
3                 10
发表于 2016-09-08 19:05:52 回复(0)
D
满足 sum( AuthorID,NumberOfCopies) <= 8 的行有4条
发表于 2015-03-17 15:38:12 回复(1)