简体   繁体   中英

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.

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.

The problem is, how to write in in 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

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.

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. 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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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