2011-09-01 66 views
0

我有以下表MySQL的 - 如何從兩個不同的表的數據到一個表

ps_ext_mod  ps_customer ps_order_detail 
---------  ----------- --------------- 
id    .....   ..... 
cust  <== email   ..... 
prod     <==  product_reference 

我需要從product_referenceemailcust的數據,並將數據添加到到prod
id是自動增量。

希望它應該是這個樣子:

ps_ext_mod 
----------- 
id | cust | product 
--------------------- 
1 | [email protected] | prod1 
2 | [email protected] | prod2 
3 | [email protected] | prod1 
4 | [email protected] | prod3 

回答

1
INSERT INTO ps_ext_mod (email, product) SELECT 
    c.email, d.product 
    FROM ps_customer c 
    INNER JOIN ps_order_detail d ON (d.customer_id = c.id) 

您將需要調整加入標準有點符合您的數據庫結構。
如果兩個表是不相關的,然後使用類似的查詢:

INSERT INTO ps_ext_mod (email, product) SELECT 
    c.email, d.product 
    FROM ps_customer c 
    CROSS JOIN ps_order_detail 
相關問題