gpt4 book ai didi

MySQL按另一张表的SUM字段查询顺序

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:54 25 4
gpt4 key购买 nike

我有 3 个表:

hotel : id, hotel_name

hotel_supplier:id, supplier_name

hotel_price:id, hotel_id, supplier_id, allotment, price

我想按酒店价格的总和(配额)降序从酒店订单中选择所有,所以结果从配额最多的酒店排序。如何做到这一点..?,任何想法感激收到...,谢谢。

sorry, i am forget one thing, there is another condition, on hotel_price if price == 0 then allotment of the row is not count in sum(allotment).

最佳答案

试试这个变体:

select h.id, h.hotel_name, sum(hp.allotment)
from hotel h join hotel_price hp on (h.id = hp.hotel_id)
group by h.id, h.hotel_name
order by 3 desc;

关于MySQL按另一张表的SUM字段查询顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10615261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com