MYSQL 垂直连接多个表以提供按日期排序的历史记录

MYSQL Joining multiple Tables vertically to give Date sorted history

我正在构建一个接口来连接 table 并在 Retool 中显示有关我们代表客户销售的二手产品的信息。

我有几个 table 都与一个产品相关。其他 table 是有关流程或状态的信息 - 即送修、售出、退回、卖家付款。

我希望得到一个 table 来提供按日期排序的产品历史记录。我可以加入所有 table 并获得单行数据,但我正在寻找垂直 table,例如:

+--+-----------+---------------+
|id|Date       |Status         |
+--+-----------+---------------+
| 1| 2020-01-01|Booked in      |
+--+-----------+---------------+
| 1| 2020-01-04|Sent for repair|
+--+-----------+---------------+
| 1| 2020-02-10|Sold           |
+--+-----------+---------------+
| 1| 2020-02-28|Returned       |
+--+-----------+---------------+

状态列将是一个 CASE 语句,由 table.

中存在的信息确定

目前比较像

+--+-----------+---------------+-----------+---------------+-----------+---------------+
|id|Date       |Status         |Date       |Status         |Date       |Status         |
+--+-----------+---------------+-----------+---------------+-----------+---------------+
| 1| 2020-01-01|Booked in      | 2020-01-01|Booked in      | 2020-01-04|Sent for repair|
+--+-----------+---------------+-----------+---------------+-----------+---------------+

总之,有没有一种方法可以根据单个产品 ID 垂直连接多个 table。

我希望这一切都是有道理的,如果有任何需要澄清的地方,我会尽力而为。

提前致谢

下面的相关 tables/data:

CREATE DATABASE Whosebug_test;

USE Whosebug_test;

CREATE TABLE    commission_sales
                (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    customer_id INT,
                    created_at TIMESTAMP DEFAULT NOW(),
                    product VARCHAR(20)
                    );
    
CREATE TABLE    sold_data
                (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    comm_no INT,
                    final_price DECIMAL(8,2),
                    sale_date DATE,
                    sale_ref VARCHAR(20),
                    created_at TIMESTAMP DEFAULT NOW(),
                    FOREIGN KEY (comm_no)
                        REFERENCES commission_sales(id)
                    );
    
    
CREATE TABLE    returned_from_buyer
                (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    comm_no INT,
                    returned_date DATE,
                    returned_notes VARCHAR(255),
                    FOREIGN KEY (comm_no)
                        REFERENCES commission_sales(id)
                    );
    
CREATE TABLE    returned_to_seller
                (   
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    comm_no INT,
                    returned_date DATE,
                    returned_notes VARCHAR(255),
                    FOREIGN KEY (comm_no)
                        REFERENCES commission_sales(id)
                    );
    
CREATE TABLE    additional_charges
                (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    comm_no INT,
                    amount DECIMAL(8,2),
                    notes VARCHAR(250),
                    paid BOOL DEFAULT 0,
                    created_at TIMESTAMP DEFAULT NOW(),
                    FOREIGN KEY (comm_no)
                        REFERENCES commission_sales(id)
                    );

还有一些假数据:

INSERT INTO     commission_sales
                (id,customer_id,product,created_at)
        VALUES  (1,72,"Teddy Bear","2020-12-10")
                ;
    
INSERT INTO sold_data
                (comm_no,final_price,sale_date,sale_ref)
        VALUES (1,25.99,"2020-12-15","AGRJOWKO")
                ;
    
    
INSERT INTO returned_from_buyer
                (comm_no,returned_date,returned_notes)
        VALUES  (1,"2020-12-29","Broken")
        ;
    
INSERT INTO returned_to_seller
                (comm_no,returned_date,returned_notes)
        VALUES  (1,"2021-01-30","Customer is Idiot")
                ;
    
INSERT INTO     additional_charges
                (comm_no,amount,notes,paid,created_at)
        VALUES  (1,10,"repair",0,"2021-01-05")
        ;

基本 JOIN 将所有数据放在一行中,但我希望数据垂直显示:

SELECT * FROM commission_sales
JOIN sold_data ON commission_sales.id = sold_data.comm_no
JOIN additional_charges ON commission_sales.id = additional_charges.comm_no
JOIN returned_from_buyer ON commission_sales.id = returned_from_buyer.comm_no
JOIN Returned_to_seller ON commission_sales.id = returned_to_seller.comm_no
;

我相信你应该像这样创建一个名为 actions 的视图。

CREATE OR REPLACE VIEW actions AS
SELECT commission_sales.id, 
       returned_from_buyer.returned_date date,
       'returned_from_buyer' action, 
       returned_from_buyer.returned_notes notes
  FROM commission_sales
  JOIN returned_from_buyer ON commission_sales.id = returned_from_buyer.comm_no
  
 UNION ALL
  
SELECT commission_sales.id, 
       returned_to_seller.returned_date date,
       'returned_to_seller' action, 
       returned_to_seller.returned_notes notes
  FROM commission_sales
  JOIN returned_to_seller ON commission_sales.id = returned_to_seller.comm_no  

  UNION ALL
  
   SELECT commission_sales.id, 
       additional_charges.created_at date,
       'additional_charges' action, 
       additional_charges.notes notes
  FROM commission_sales
  JOIN additional_charges ON commission_sales.id = additional_charges.comm_no  ;

这个包含 UNION ALL 操作的视图是一种类型转换操作。它从这三个 table 中提取数据并将它们放入通用格式。将 VIEW 视为虚拟 table.

然后您可以像这样从中检索数据。

SELECT * FROM actions ORDER BY id, date;

或者您可以将其加入系统中的其他 table。这是一个fiddle.