MySQL表加入数据库?

时间:2015-02-10 08:03:11

标签: mysql phpmyadmin

我想知道mysql如何构建一个新表来显示我想要的数据?

  

目前有购买表和产品表

我的产品表:

=====Product=====
field productID
field category
field brand
field name
field createtime

我的购买表:

=====Purchase=====
field UID 
field productID 
field username
field email
field purchaseDate

我希望得到的结果表:

=====OrderList=====
field UID 
field productID
field category
field brand
field name 
field username
field email
field purchaseDate

我怀疑,一旦在购买表中插入新记录,mysql OrderList表会在数据库中自动更新吗?

  

以下是解决我问题的示例方法吗?

SELECT Purchase.UID, Purchase.productID, Product.category, Product.brand,
 Product.name, Purchase.username, Purchase.email, Purchase.purchaseDate
FROM Product
LEFT JOIN Purchase
ON Product.productID=Purchase.productID

2 个答案:

答案 0 :(得分:0)

如果要自动更新OrderList表,则需要创建一个视图,而不是静态表:

create or replace view OrderList as
    SELECT Purchase.UID, Purchase.productID, Product.category, Product.brand,
 Product.name, Purchase.username, Purchase.email, Purchase.purchaseDate
FROM Product
LEFT JOIN Purchase
ON Product.productID=Purchase.productID
ORDER BY Customers.CustomerName;

答案 1 :(得分:0)

使用" VIEW",例如:

CREATE VIEW OrderList
AS
     SELECT Purchase.UID,
            Purchase.productID,
            Product.category,
            Product.brand,
            Product.name,
            Purchase.username,
            Purchase.email,
            Purchase.purchaseDate
       FROM   Product, Purchase
       WHERE  Product.productID = Purchase.productID
   ORDER BY Customers.CustomerName