开发者

MySQL - How to get data from two different tables into one table

I have the following tables

ps_ext_mod     ps_customer    ps_order_detail
---------      -----------    ---------------
id   开发者_如何学Go          .....          ..... 
cust      <==  email          .....
prod                  <==     product_reference

I need to add the data from email to cust and the data from product_reference into to prod.

id is auto increment.

Hopefully it should look something like this:

ps_ext_mod
-----------
id | cust   | product
---------------------
1  | a@a.com | prod1
2  | a@a.com | prod2
3  | b@b.com | prod1
4  | c@c.com | prod3


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) 

You will need to tweak the join criterion a bit to match your database structure.
If the two tables are NOT related, then use a query like:

INSERT INTO ps_ext_mod (email, product) SELECT
   c.email, d.product
   FROM ps_customer c
   CROSS JOIN ps_order_detail
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜