我这里加入了 3 张 table 。其中包括客户、订单和客户订购的商品。它们被命名为:CustomersTbl、OrdersTbl、OrderedItemTbl。
我正在排列所有订单及其总价,并显示下订单的客户的姓名。
select * from productstbl;
select * from ordereditemtbl;
select * from orderstbl;
select
ordereditemtbl.ItemID,
customerstbl.CustomerID,
customerstbl.FirstName,
orderstbl.OrderID,
orderstbl.OrderDate,
sum(ordereditemtbl.amount * productstbl.Price) as totalPrice
from
ordereditemtbl
inner join
ProductsTbl ON ordereditemtbl.productID = productstbl.productID
inner join
orderstbl ON ordereditemtbl.orderID = ordereditemtbl.orderID
inner join
customerstbl ON customerstbl.customerID = orderstbl.customerID#'2'
group by ordereditemTbl.OrderID
order by totalPrice desc;
现在我想单独显示 TotalPrice 最高的订单。我怎样才能得到它的最大值?有没有办法可以使用 MAX() 函数?
最佳答案
让我们看看。看看这是否有效:
SELECT * FROM
(
select
ordereditemtbl.ItemID,
customerstbl.CustomerID,
customerstbl.FirstName,
orderstbl.OrderID,
orderstbl.OrderDate,
sum(ordereditemtbl.amount * productstbl.Price) as totalPrice,
@rownum AS row_number
from
ordereditemtbl
inner join
ProductsTbl ON ordereditemtbl.productID = productstbl.productID
inner join
orderstbl ON ordereditemtbl.orderID = ordereditemtbl.orderID
inner join
customerstbl ON customerstbl.customerID = orderstbl.customerID#'2'
inner join
(SELECT @rownum := 0) dummy
group by ordereditemTbl.OrderID
ORDER BY totalPrice desc;
)
WHERE row_number=0
这会分配行号并选择顶行。
干杯, 公羊
关于MySql:返回连接表中的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313942/