如何使用mysql获得总订单价格?

时间:2014-03-30 18:37:05

标签: mysql

有两个表,一个是订单,第二个是order_details,

order

order_id    order_name
1           shoes
2           wallet
3           socks
4           bats

order_details

order_details_no  order_id(foregin key)  order_price
        1              1                   25
        2              1                   55
        3              2                   65
        4              4                   30
        5              4                   60

我的问题是,我想要的结果集包括order_id,订单总价格按升序排列(例如,订单总数为80,订单总数为90)

如何获得这个?

2 个答案:

答案 0 :(得分:2)

select order_id, 
       sum(order_price) as total_sum
from order_details
group by order_id
order by total_sum asc

答案 1 :(得分:-1)

Select 
    order.order_name, 
    sum(order_details.order_price) as price 
from order 
     join order_details 
       on order_details.order_id=order.order_id 
group by 
    order.order_id 
order by 
    price desc