简体   繁体   中英

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             .....          ..... 
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

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