- 1
- 0
- 约4.24千字
- 约 19页
- 2019-11-06 发布于广东
- 举报
阶段2 参考答案 需求说明 使用NOT IN子查询统计非管理员角色的本年度月均购卡消费金额 实现思路 通过子查询获得所有用户的购卡信息(包括卡号、金额、时间、用户名 分组计算每年度各月的平均消费金额,在分组前还需要使用not in 过滤掉管理员的销售金额 阶段小结 共性问题集中讲解 子查询结果集作为虚表参与外部查询 相关子查询的执行方式 ANY的使用方法 总结 总结并布置作业 深入SQL高级子查询 第三章 上机 作业点评 理论作业 局部变量的声明使用declare关键字声明,赋值使用SET 和 SELECT SET一般将已知的的值赋给变量,SELECT是把查询的结果值赋给变量 BREAK从当前循环中跳出;RETURN结束执行当前代码块,RETURN后面所有的语句都不会再执行;GOTO跳转到标签指定的代码行继续执行。BREAK、RETURN、GOTO都可以从WHILE循环中跳出来 上机作业 select 产品编号,sum(case month(日期) when 1 then 数量 else 0 end) 1月, sum(case month(日期) when 2 then 数量 else 0 end) 2月, sum(case month(日期) when 3 then 数量 else 0 end) 3月, sum(case month(日期) when 4 then 数量 else 0 end) 4月, sum(case month(日期) when 5 then 数量 else 0 end) 5月, sum(case month(日期) when 6 then 数量 else 0 end) 6月, sum(case month(日期) when 7 then 数量 else 0 end) 7月, sum(case month(日期) when 8 then 数量 else 0 end) 8月, sum(case month(日期) when 9 then 数量 else 0 end) 9月, sum(case month(日期) when 10 then 数量 else 0 end) 10月, sum(case month(日期) when 11 then 数量 else 0 end) 11月, sum(case month(日期) when 12 then 数量 else 0 end) 12月 from OrderDetails group by 产品编号 使用Case-End实现的片段代码 上机内容 使用基本子查询 在子查询中使用IN/EXISTS 在子查询中使用ALL/ANY 使用相关子查询 上机目标 实现用户购买游戏卡过程 使用IN、ANY子查询为用户升级角色 使用子查询统计滞销的游戏卡类型和热销的游戏卡类型 使用子查询统计目前还没有任何销量的游戏卡类型 使用NOT IN和相关子查询统计非管理员角色本年度月均购卡消费金额 统计各种游戏卡月平均销售总额 上机效果演示 用户购买游戏卡成功! 用户角色升级成功! 当年每月销售统计 阶段1 训练内容 使用基本子查询 在UPDATE语句中使用子查询 在INSERT语句中使用子查询 使用IN/NOT IN的子查询 需求说明 实现用户“jack123”购卡流程:用户“jack123”想一次购买“NBA篮球卡”5张,要求实现此用户的购卡流程 阶段1 完成时间:50分钟 实现思路 购买游戏卡的过程一共需要6步 查询用户购买的卡类型的卡数量是否足够用户购买 检查用户账户的余额是否足够购买这些 将购买的游戏卡保存到购物历史记录中 将购买的游戏卡信息保存到购物车 更新用户的账户余额 更新游戏卡的状态 阶段1 if(@cnt=5) begin declare @balance money,@requiremoney money select @balance =balance from UserInf where UserName =jack123 select @requiremoney =5*price from CardType where Id= (select id from CardType where CardTypeName =NBA篮球卡) set @requiremoney =@requiremoney*(select discount from RoleInf where id= (select UserRoleId from UserInf where UserName =jack123)) if(@balance=@requiremoney) begin dec
原创力文档

文档评论(0)