简体   繁体   English

使用多个内部联接

[英]Using multiple inner joins

I have four tables: users , orders , orders_product and products . 我有四个表: usersordersorders_productproducts They are connected to each other by foreign key 它们通过外键相互连接

user tables contains: id , name , email and username . 用户表包含: idnameemailusername

product table contains: id , product_name , product_description and product_price product表包含: idproduct_nameproduct_descriptionproduct_price

orders table contains: id , u_id(foreign key) . orders表包含: idu_id(foreign key)

orders_product table contains: id , product_id(foreign key) , order_id(foreign key) . orders_product表包含: idproduct_id(foreign key)order_id(foreign key)

Now I was trying to fetch the name of a user with the total price of a particular order that he has placed. 现在我试图用他所放置的特定订单的总价来获取用户的名称。

The maximum I could went for was something like this: 我能达到的最大值是这样的:

SELECT prod.order_id,
       SUM(product_price) AS Total
FROM products
INNER JOIN
  (SELECT orders.id AS order_id,
          orders_product.product_id
   FROM orders
   INNER JOIN orders_product ON orders.id = orders_product.order_id
   WHERE order_id=1) AS prod ON products.id = prod.product_id;

It showed me total price of a particular order. 它显示了特定订单的总价。 Now I have two questions: 现在我有两个问题:

  1. Is that query correct. 这个查询是否正确。 It looks like a very long query. 它看起来像一个很长的查询。 Can the same result be achieved with a smaller one? 用较小的一个可以获得相同的结果吗?

  2. How to fetch the name of a user with the total price of a particular order that he has placed. 如何使用已放置的特定订单的总价格获取用户的名称。

Your query is correct for one order, but it can be improved: 您的查询对于一个订单是正确的,但可以改进:

  • Don't use a subquery unless necessary. 除非必要,否则不要使用子查询。 In MySQL this introduces additional overhead. 在MySQL中,这引入了额外的开销。
  • You are only looking at one order, which seems on the light site. 你只看一个订单,这似乎在灯光网站上。 You should remove the where clause. 你应该删除where子句。
  • You should be using a group by because you want aggregation. 您应该使用group by因为您需要聚合。
  • You need to join in the user table to get the name. 您需要加入用户表以获取名称。

I also added table aliases (abbreviations for table names). 我还添加了表别名(表名的缩写)。 This makes the query a bit more readable: 这使查询更具可读性:

SELECT u.name, SUM(p.product_price) as Total
FROM orders_product op INNER JOIN
     orders o
     ON o.id = op.order_id  INNER JOIN
     products p
     ON p.id = op.product_id INNER JOIN
     users u
     on o.userid = u.id
WHERE op.order_id = 1
GROUP BY u.name;

Hi some addition to @Gordon Linoff your query seems ok. 您好@Gordon Linoff的一些补充您的查询似乎没问题。 if you store your price data in order_products it will be good and some benefit, one of these benefit is aggregation will be simple. 如果您将价格数据存储在order_products中,那将是好的和一些好处,其中一个好处是聚合将很简单。 Second benefit if product price change it will not affect to order. 第二个好处,如果产品价格变化,它不会影响订单。

Your SQL is wrong. 你的SQL错了。 Because You want to calculate specific to user. 因为您想要特定于用户计算。 But your SQL is specific to Order. 但是您的SQL特定于Order。 Your SQL will give result for One Order. 您的SQL将为One Order提供结果。 Please make it User Specific by giving user name or what ever is unique. 请通过提供用户名或任何独特的内容使其成为用户特定的。

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

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