1,3号顾客发票的最大金额
select max(invoice_total)
from invoices
where client_id=3

2,筛选大于3号顾客的所有发票(首先是写出三号顾客最大发票额的查询,其次是写大于3号最大发票额的查询)

做法一:
select *
from invoices
where invoice_total>(
select max(invoice_total)
from invoices
where client_id=3)

做法二:
首先选择蓝色部分,记住括号也要选,得到该结果不止一个

然后全选,执行,发现
Error Code: 1242. Subquery returns more than 1 row
因此需要改为,意思是需要大于()里面的所有值,也就是大于()里面最大值
select *
from invoices
where invoice_total>all(
select invoice_total
from invoices
where client_id=3)
文章讲述了如何在SQL中找出3号顾客的最大发票金额,并解决在筛选大于这个金额的发票时遇到的多行子查询返回多行的问题,提供了两种做法:一种是使用大于符号直接比较,另一种是使用`all`函数获取所有大于子查询最大值的发票。
7880

被折叠的 条评论
为什么被折叠?



