简体   繁体   English

SQL 使用选择连接查询

[英]SQL Joining query using Selection

I have one table of tbl_order where columns are:我有一张 tbl_order 表,其中列是:

  • id_order id_order
  • username用户名
  • delivery_date etc...交货日期等...

and other table tbl_order_foods where columns are:和其他表 tbl_order_foods 列是:

  • id_order id_order
  • id_food id_food
  • food_quantity etc食物数量等

Here the sample pic of tbl_order table这里是 tbl_order 表的示例图片

在此处输入图像描述

and tbl_order_foods table和 tbl_order_foods 表

在此处输入图像描述

Here i am want to select all the foods with quantity with price and unit based on delivery_date.在这里,我想 select 所有食物的数量,价格和单位基于交货日期。 Ex: Suppose On 2020-04-18 there are 3 orders where foods: tomato's are ordered total 15 kg (3 orders * 5 quantity * food_min_unit_amount * unit) and so on others foods.例如:假设在 2020 年 4 月 18 日有 3 个订单,其中食品:西红柿的订单总量为 15 公斤(3 个订单 * 5 个数量 * food_min_unit_amount * 单位)等等其他食品。

how i can get the foodlist of total ordered quantity based on delivery_date我如何根据delivery_date获取总订购量的食物清单

The method you need is the GROUP BY keyword.您需要的方法是 GROUP BY 关键字。

SQL grouping works on the given attributes. SQL 分组适用于给定的属性。 According to your question this would be the delivery_date .根据您的问题,这将是delivery_date In the projection (the attributes following the SELECT keyword) you then can use attributes you state after the GROUP BY keyword and aggregation functions eg SUM and MAX.在投影(SELECT 关键字之后的属性)中,您可以在 GROUP BY 关键字和聚合函数(例如 SUM 和 MAX)之后使用 state 的属性。

Based on your question you could get the total price for all orders on the given date like this:根据您的问题,您可以获得给定日期所有订单的总价,如下所示:

SELECT order.delivery_date, SUM(food.food_quantity) as amount, SUM(food.food_total_price) as revenue
FROM tbl_order as order 
INNER JOIN tbl_order_foods as food ON order.id_order = food.id_order
GROUP BY order.delivery_date

This would result in a list like this:这将产生如下列表:

date | amount | revenue
------------------------
05.01|     10 |  800.00

I don't know whether this is what you wish.我不知道这是不是你想要的。 If you also want to split it into order positions then you would GROUP BY the food_name as well and add it to the projection which then results in the SUM grouped by the orders on a given day of a given product.如果您还想将其拆分为订单位置,那么您也可以按food_name并将其添加到投影中,然后生成按给定产品的给定日期的订单分组的 SUM。

SELECT order.delivery_date, food.food_name, SUM(food.food_quantity) as amount, SUM(food.food_total_price) as revenue
FROM tbl_order as order 
INNER JOIN tbl_order_foods as food ON order.id_order = food.id_order
GROUP BY order.delivery_date, food.food_name

Which would result in something like this.这会导致这样的事情。

date | food_name | amount | revenue
-----------------------------------
05.01|  Tomato   |   10   |  800.00
05.01|  Apple    |   5    |  400.00

Check out GROUP BY with the SUM function, a good article from Tutorialspoint.com is here .使用 SUM function 查看 GROUP BY,这是来自 Tutorialspoint 的一篇好文章。com在这里 The only difference is that you are performing a JOIN to create the resulting table.唯一的区别是您正在执行 JOIN 来创建结果表。

  SELECT DISTINCT [f.id_food], [o.delivery_date], SUM(f.food_quantity) as [delivered_sum_qty], SUM(f.food_total_price) as [sum_food_total_price]
  FROM tbl_order o LEFT JOIN tbl_order_foods f
  ON [o.id_order] = [f.id_order]
  GROUP BY [o.delivery_date];

If you need it, a pretty clear explanation of how to use JOIN is here as well.如果你需要它,这里也有一个关于如何使用 JOIN 的非常清晰的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM