数据库实验5.2详解(仅供校友参考)


同样的,我们先读题,题目非常简短,要求我们计算每一位顾客的订单数,那么我们自然会想到去count一下表shoporder中的ordid。有思路后,那我们就直接开始来答题。

解:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P3sqz6kW-1650453303294)(C:\Users\C4n\AppData\Roaming\Typora\typora-user-images\image-20220420184315442.png)]

然后我们发现csurname被标了红色下曲线。

观察一下数据库关系图(直接在sql server中建立),原来csurname是在表custome中,那么我们自然会想到要对两表进行连接。

select customer.custid,csurname,count(ordid)“total orders”
from shoporder,customer
where shoporder.custid=customer.custid
group by customer.custid,csurname;

查询结果如下图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jBH6Y4MD-1650453303295)(C:\Users\C4n\AppData\Roaming\Typora\typora-user-images\image-20220420184733394.png)]

外连接做法

和原题目对照一下,发现我们还是有些不一样是不是,有看上期讲解(5.1)的同学应该就想到了,使用外连接就可以处理这个问题,我们写一下:

select customer.custid,csurname,count(ordid)“total orders”
from shoporder right outer join customer on(shoporder.custid=customer.custid )
group by customer.custid,csurname;

派生表做法

虽然我们这样已经用右外连接完美解决这道题了,但我们这期讲解总不能没有新内容是吧。下面我介绍一种基于派生表查询的方法来完成这道题。

1.首先,我们先把表shoporder中的全部数据选出来

select
custid,COUNT(shoporder.ordid) “total orders”
from shoporder
group by custid

2.再把customer中的需要的数据选出来,

即把custid不在shoporder中却在customer中的数据给选出来

select
custid,0 “total orders”
from customer
group by custid
having custid not in (select custid from shoporder)

3.然后再把他们进行一下union,即

select
custid,COUNT(shoporder.ordid) “total orders”
from shoporder
group by custid
union
select
custid,0 “total orders”
from customer
group by custid
having custid not in (select custid from shoporder)

4.然后我们把这个union好的表写进from中,即作为派生表,命名为a

select a.custid,“total orders”
from (
select
custid,COUNT(shoporder.ordid) “total orders”
from shoporder
group by custid
union
select
custid,0 “total orders”
from customer
group by custid
having custid not in (select custid from shoporder)
) as a;

先查询一下看看,可以看到,我们已经大致实现了我们的想法,但还少查了csurname。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NNblKMOE-1650453303295)(C:\Users\C4n\AppData\Roaming\Typora\typora-user-images\image-20220420190905446.png)]

5.增加列csurname,我们直接利用custid连接customer表和a表(派生表)即可。

select a.custid,csurname,“total orders”
from customer,(
select
custid,COUNT(shoporder.ordid) “total orders”
from shoporder
group by custid
union
select
custid,0 “total orders”
from customer
group by custid
having custid not in (select custid from shoporder)
) as a
where customer.custid=a.custid;

最终查询结果如下图:
在这里插入图片描述

那么本期讲解就到这里了,我们下期再见!

case when做法

其实使用case when也可以做这道题,不过我写得有些乱,看上去逻辑没有很清晰,有兴趣的同学的自行了解一下
select
customer.custid,csurname,case when customer.custid not in (select
custid from shoporder) then 0 else COUNT(shoporder.ordid) end “total
orders” from shoporder,customer where (case when customer.custid not
in(select custid from shoporder) then 1 when
customer.custid=shoporder.custid then 1 end )=1 group by
customer.custid,csurname order by customer.custid;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是基于实验5.2的界面布局代码,并实现了相应菜单完成按钮的功能: ```python import tkinter as tk def func(): label.config(text="Hello World!") def about(): about_window = tk.Toplevel(root) about_window.title("关于") about_label = tk.Label(about_window, text="这是一个关于窗口") about_label.pack() def open_file(): file_window = tk.Toplevel(root) file_window.title("打开文件") file_label = tk.Label(file_window, text="在这里打开文件") file_label.pack() def save_file(): save_window = tk.Toplevel(root) save_window.title("保存文件") save_label = tk.Label(save_window, text="在这里保存文件") save_label.pack() root = tk.Tk() root.title("实验5.2") root.geometry("300x200") menu = tk.Menu(root) file_menu = tk.Menu(menu, tearoff=False) file_menu.add_command(label="打开", command=open_file) file_menu.add_command(label="保存", command=save_file) file_menu.add_separator() file_menu.add_command(label="退出", command=root.quit) menu.add_cascade(label="文件", menu=file_menu) help_menu = tk.Menu(menu, tearoff=False) help_menu.add_command(label="关于", command=about) menu.add_cascade(label="帮助", menu=help_menu) root.config(menu=menu) button = tk.Button(root, text="点击我", command=func) button.pack(pady=20) label = tk.Label(root, text="") label.pack() root.mainloop() ``` 这段代码中,我们添加了一个名为“文件”的下拉菜单,其中包括三个选项:“打开”、“保存”和“退出”。当用户点击“打开”选项时,会弹出一个新的窗口,提示用户在这里打开文件;当用户点击“保存”选项时,会弹出一个新的窗口,提示用户在这里保存文件。当用户点击“退出”选项时,程序将会退出。 同样地,我们也添加了一个名为“帮助”的下拉菜单,其中包括一个“关于”选项。当用户点击“关于”选项时,会弹出一个新的窗口,提示用户这是一个关于窗口。 此外,我们还添加了一个按钮和一个标签。当用户点击按钮时,标签的文本将会变成“Hello World!”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值