简体   繁体   English

MySQL,一张表连接两张表?

[英]MySQL, Join two tables with one table?

I have one table customer , and one bill , and one sell .我有一位customer ,一张bill ,一张sell单。

Customer table客户表

id-----name

Bill table账单表

id-----customer_id

Sell table卖表

id-----customer_id-----bill_id-----qtt-----price

A customer can have the records in the sell table with customer_id , and also have the bill record in the bill table with customer_id and this bill record has record in the sell table with bill_id .一个客户可以在sell customer_id bill customer_id bill bill sell bill_id This means a customer can have direct or indirect (in this case by passing the bill table) with sell table.这意味着客户可以直接或间接(在这种情况下通过bill表)与sell表。

Now how to join tables that retrieve the total sell of a customer with ascending or deciding order?现在如何连接以升序或决定顺序检索客户总销售额的表?

Any idea?任何想法?

I have tried many ways for example something like below, but none of them was working:我尝试了很多方法,例如下面的方法,但没有一个有效:

SELECT 
    sell.id AS sell_id,
    customer.id,
    bill.id AS bill_id,
    customer.`name`,
    sell.quantity*sell.price AS sell_price
FROM
    customer_tb customer
    JOIN bill ON bill.customer_id = customer.id
    JOIN sell ON sell.customer_id = customer.id OR sell.bill_id = bill.id

NOTE: In case of bill table has a record in the sell table, the customer_id column is NULL and also same for the customer sell record the bill_id is NULL , this means in the sell table in the same entry only one of the ( customer_id , bill_id ) column has value.注意:如果bill表在sell表中有一条记录, customer_id列是NULL并且客户sell记录的bill_id也是NULL ,这意味着在同一条目中的sell表中只有一个( customer_idbill_id ) 列有值。

You have the customer column in your sell tables, you don't need to join with bill table.您的销售表中有客户列,您不需要加入账单表。

SELECT 
   customer.id,
   customer.name,
   SUM(sell.quantity * sell.price) AS total_amount
FROM customer_tb as customer
INNER JOIN sell 
   On sell.customer_id = customer.id
GROUP BY customer.id

You have to group your rows by the customer to get their sum independently您必须按客户对行进行分组才能独立获得总和

Your challenge is to look up the correct Customer.name for each Sell row.您的挑战是为每个 Sell 行查找正确的 Customer.name。

You need to join Sell to Customer, and also join Sell to Bill to Customer.您需要加入 Sell to Customer,还需要加入 Sell to Bill to Customer。

How to do this?这个怎么做?

        FROM Sell s
   LEFT JOIN Cust c1 ON s.customer_id = c1.id
   LEFT JOIN Bill b on s.bill_id = b.id
   LEFT JOIN Cust c2 ON b.customer_id = c2.id

That gives us:这给了我们:

  • s.id: the Sale id s.id:销售 id
  • s.quantity, s.price: the business details of the Sale s.quantity, s.price:销售的业务详情
  • c1.name: the Customer name directly from the Sale's customer_id c1.name:直接来自销售的 customer_id 的客户名称
  • c2.name: the Customer name indirectly through the Sale's bill_id c2.name:通过销售的 bill_id 间接获得的客户名称

Because we use LEFT JOINs, either name can be NULL depending on which ids are null.因为我们使用 LEFT JOIN,所以任何一个名称都可以是 NULL,具体取决于哪个 id 是 null。 And, I believe you favor using the direct name over the indirect name if you have both.而且,如果您同时使用两者,我相信您更喜欢使用直接名称而不是间接名称。

That means we use COALESCE() to choose one name from the two.这意味着我们使用COALESCE()从两个名称中选择一个。 Something like this像这样的东西

      SELECT s.id, s.Quantity, s.Price,
             COALESCE (c1.name, c2.name) name,
             COALESCE (c1.id, c2.id) customer_id
        FROM Sell s
   LEFT JOIN Cust c1 ON s.customer_id = c1.id
   LEFT JOIN Bill b on s.bill_id = b.id
   LEFT JOIN Cust c2 ON b.customer_id = c2.id

But, if you have a chance to rework this database before you put a ton of data into it, please consider redesigning things to get rid of this ambiguity.但是,如果您有机会在将大量数据放入其中之前重新设计该数据库,请考虑重新设计以消除这种歧义。 It's going to drive you mad and cost you bigger servers if you have to make it work well with megarows.如果你必须让它与 megarows 一起工作,它会让你发疯并花费你更大的服务器。

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

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