简体   繁体   English

带子查询的SQL查询,连接多个表-MySQL

[英]SQL query with subqueries, joining multiple tables - MySQL

The tables are: 这些表是:

1) Producers: id, name
2) Products: id, name, price, category_id, producer_id
3) Customers: id, name, surname, age, country_id
4) Customer_orders: id, customer_id, product_id, quantity, discount, order_date

I need to find the prodcuer, who's income was the highest, in other words: find a producer for which the sum of all prodcut price * quantity - discount is the highest. 我需要找到收入最高的产品生产者,换句话说:找到一个生产者,其所有产品price * quantity - discount的总和最高。

I suppose, that I should join table customer_orders with product_id column on products , and products with producers table on column id and then calculate the appropriate income for all producers and then find the maximum value. 我想,我应该将表customer_ordersproducts上的product_id列连接,并将productsproducers表的id列连接,然后为所有生产者计算适当的收入,然后找到最大值。

The problem is, how to write in in MySql. 问题是,如何在MySql中编写。

You can start with something like this: 您可以从以下内容开始:

SELECT SUM(price - discount) income, producers.name name FROM customer_orders LEFT JOIN products ON customer_orders.product_id = products.id LEFT JOIN producers ON producers.id = products.producer_id GROUP BY producer_id

I'm not sure if that will work exactly how I typed it but it should be a starting point. 我不确定这是否会完全按照我的输入方式工作,但这应该是一个起点。

Here are some helpful links: 以下是一些有用的链接:

Left Joins Sum Function Left Joins Sum Function

Please note: I'm currently working on a server where the tables are structured like this, and it's super slow and a pain to get anything done. 请注意:我目前正在服务器上,其中表的结构是这样的,它非常慢,很难完成任何事情。 I'd encourage you to consider at least adding the total order about and the producer_id to the customer_orders table to make it a lot easier. 我鼓励您考虑至少将总订单和producer_idcustomer_orders表中,以使其更加容易。

What you want to do is summarize the order values per producer. 您要做的是总结每个生产者的订单值。 The order value is defined by price, quantity and discount. 订单价值由价格,数量和折扣定义。 Since only the last 2 can be found in the customer_orders table, you'll have to join the Products table. 由于在customer_orders表中只能找到最后2个,因此您必须加入Products表。 This can be done with the following query: 可以使用以下查询完成此操作:

select * from customer_orders
inner join products on products.id = customer_orders.product_id

But since we're supposed to group them by the producer, we'd need the name or id of the producer as well, so we also have to join the producers table: 但是由于我们应该按生产者对它们进行分组,因此我们也需要生产者的名称或ID,因此我们还必须加入生产者表:

select * from customer_orders
inner join products on products.id = customer_orders.product_id
inner join producers on producers.id = products.producer_id

Now you're selecting all the information you need. 现在,您正在选择所需的所有信息。 To do the calculations you need to replace the asterisk with: 要进行计算,您需要将星号替换为:

(customer_orders.quantity * customer_orders.discount) - products.price

which will give you the value of each order. 这将为您提供每个订单的价值。

To sum all the order for a given producer, you need to wrap the above line in a SUM() function, and you need to add a GROUP BY clause at the end of the query. 要汇总给定生产者的所有订单,您需要将以上行包装在SUM()函数中,并且需要在查询的末尾添加GROUP BY子句。

select SUM( (customer_orders.quantity * customer_orders.discount) - products.price) as value, producers.name from customer_orders
inner join products on products.id = customer_orders.product_id
inner join producers on producers.id = products.producer_id
group by producers.name

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

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