MySql - Create view to read from Multiple Tables

前端 未结 2 746
不知归路
不知归路 2021-01-28 18:11

I have archived some old line items for invoices that are no longer current but still need to reference them. I think I need to create a VIEW but not really understanding it. Ca

相关标签:
2条回答
  • You can use UNION :

    SELECT a.* FROM a 
      UNION
    SELECT b.* FROM b;
    

    You just need to have the same number and type of column in your different queries. As far as I remember, you can add test in sub-queries, but I'm not sure you can order on the global result.

    http://dev.mysql.com/doc/refman/4.1/en/union.html

    0 讨论(0)
  • 2021-01-28 18:42

    You can use the MERGE Storage Engine to create a virtual table that's the union of two real tables:

    CREATE TABLE Invoice_LineItem_All 
    (
      `LineItem_ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
      `LineItem_ChargeType` VARCHAR(64) NOT NULL DEFAULT '',
      `LineItem_InvoiceID` INT(11) UNSIGNED DEFAULT NULL,
      `LineItem_Amount` DECIMAL(11,4) DEFAULT NULL,
      `LastUpdatedAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      KEY (`LineItem_ID`),
      KEY `LastUpdatedAt` (`LastUpdatedAt`),
      KEY `LineItem_InvoiceID` (`LineItem_InvoiceID`)
    ) ENGINE=MERGE UNION=(Invoice_LineItem_Archived, Invoice_LineItem);
    
    0 讨论(0)
提交回复
热议问题