Today my question is how would I go about creating a view in a MySQL database that uses more than two tables?
Here is my query (it works) I am not looking to change my current query, mostly looking for a nice reference with examples on this topic.
CREATE OR REPLACE VIEW vw_itemsPurchased AS
SELECT `tbl_buyers`.`fldPrimaryKey` as fldFKeyBuyer, `tbl_buyers`.`fldEmail` as fldBuyerEmail, `tbl_buyers`.`fldAddressStreet`, `tbl_buyers`.`fldAddressCity`, `tbl_buyers`.`fldAddressState`, `tbl_buyers`.`fldAddressZip`, `tbl_buyers`.`fldAddressCountry`, `fldPaymentCurrency`, `fldPaymentGross`, `fldPaymentStatus`, `fldReceiverEmail`, `fldTransactionId`
FROM `tbl_transactions` INNER JOIN `tbl_buyers`
ON `tbl_transactions`.`fldFKeyBuyer` = `tbl_buyers`.`fldPrimaryKey`
Thanks for your time!
To use more than two tables, you simply continue adding
JOINstatements to connect foreign keys. Adapting your code to add an imaginary third tabletbl_productsmight look like this:In the above method, the first and second tables relate, and the first and third tables relate. If you have to relate
table1->table2andtable2->table3, list multiple tables in theFROMand relate them in theWHERE. The below is just for illustration and doesn’t make much sense, as you probably wouldn’t have a customer id in the same table as a product price.