简体   繁体   中英

MySql - Create view to read from Multiple Tables

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. Can someone help so I can run a query to pull the invoice and then the total of all the line items assigned (no matter what table the items are in)?

CREATE TABLE `Invoice` (
  `Invoice_ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `Invoice_CreatedDateTime` DATETIME DEFAULT NULL,
  `Invoice_Status` ENUM('Paid','Sent','Unsent','Hold') DEFAULT NULL,
  `LastUpdatedAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`ID`),
  KEY `LastUpdatedAt` (`LastUpdatedAt`)
) ENGINE=MYISAM DEFAULT CHARSET=latin1

CREATE TABLE `Invoice_LineItem` (
  `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,
  PRIMARY KEY (`LineItem_ID`),
  KEY `LastUpdatedAt` (`LastUpdatedAt`),
  KEY `LineItem_InvoiceID` (`LineItem_InvoiceID`)
) ENGINE=MYISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

CREATE TABLE `Invoice_LineItem_Archived` (
  `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,
  PRIMARY KEY (`LineItem_ID`),
  KEY `LastUpdatedAt` (`LastUpdatedAt`),
  KEY `LineItem_InvoiceID` (`LineItem_InvoiceID`)
) ENGINE=MYISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

Typically I would just run the following query to get the amount due on the invoices

SELECT
    Invoice_ID,
    Invoice_CreatedDateTime,
    Invoice_Status,
    (SELECT SUM(LineItem_Amount) AS totAmt FROM Invoice_LineItem WHERE LineItem_InvoiceID=Invoice_ID) AS Invoice_Total
FROM
    Invoice
WHERE
    Invoice_Status='Sent'

Also how can I select all the line items from both tables in one query?

SELECT
    LineItem_ID,
    LineItem_ChargeType,
    LineItem_Amount
FROM
    Invoice_LineItem
WHERE
    LineItem_InvoiceID='1234'

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);

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

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