如何获得总和(具体来说)

时间:2018-09-24 07:51:22

标签: mysql

Fe_Order_Items

fe_order_items_id
fe_order_specification_id
fe_users_id
fe_menu_items_id
fe_order_items_quantity
fe_order_items_time_order

Fe_Order_Specification

fe_order_specification_id
fe_users_id
fe_users_address_id
fe_users_email_address_id
fe_users_phone_number_id
fe_menu_items_id
fe_menu_items_quantity
fe_payment_customer_payment_order_total
fe_payment_customer_payment_acceptance
fe_payment_customer_payment_acceptance_by_food_express
fe_payment_customer_method_of_payment
fe_customer_service_order_process_start
fe_delivery_system_order_signed_by_customer_before_delivery
fe_delivery_system_order_order_delivered_to_customer
total

我运行的查询(导师帮助了我,因为仍然很难拿出一些东西来计算总数(已订购的所有插入菜单项):

SELECT fe_order_specification.fe_order_specification_id, 
    fe_menu_items.fe_menu_items_name, 
    SUM(fe_menu_items.price `fe_menu_items_quantity`) AS Total 
FROM fe_order_items
INNER JOIN fe_menu_items          ON fe_order_items.fe_menu_items = fe_menu_items.fe_menu_items_id
INNER JOIN fe_order_specification ON fe_order_items.`fe_order_specification` = fe_order_specification.`fe_order_specification_id`
WHERE fe_order_items.fe_order_specification=1
GROUP BY fe_order_items.fe_menu_items

mysql并没有给我带来可观的总和,而是给了我一些法语(和英语((我的设置是在英语UK上,所有内容都是英语)):

  

1054-“ con_dans”字段列表中的“ fe_order_specification.fe_order_specification_id”冠军

1 个答案:

答案 0 :(得分:0)

我希望查询看起来如下:

SELECT SUM(mi.price * oi.quantity)
FROM order_items AS oi
JOIN menu_items AS mi ON mi.id = oi.menu_item_id
WHERE oi.order_id = @ORDERID

但是,恐怕这与您当前的数据模型不匹配...我会更改...

  • 与订单有很多关系的订单商品
  • 每个订单项只有一个菜单项,与多个菜单项无关。这是使您很难对所显示的表格进行查询的关键区域,但是如果您更改为要订购的项目只有一个菜单项,则变得很容易

数据模型如下所示(请注意,我删除了对查询不重要的字段):

order_items:

id (PK) (AUTO INCREMENTED) (INT)
order_id (FK) (INT)
menu_item_id (FK) (INT)
quantity (INT)

菜单项:

id (PK) (AUTO INCREMENTED) (INT)
price (DECIMAL)

其他说明:

  • 我会将user_id存储在order表中(尚未定义)
  • 我将菜单类型存储在菜单项上:type (ENUM) ['dinner','dessert']
  • 如果需要控制应以编程方式完成的这些项目的数量