Mysql – How to connect use INSERT query using SELECT query in theSQL

insertMySQLselect

I have 2 tables, cce_sa_fa & cce_activity

I used the following line to get the cce_sa_fa not included in cce_activity

select id from cce_sa_fa where id NOT IN (select safa from cce_activity)

Using this query, I got 113 rows. Now I need to INSERT these ids into cce_activity. Here the id needs to be inserted and other columns are with fixed value. Usual insert query for cce_activity is as follows.

INSERT INTO `cce_activity` (`id`, `safa`, `name`, `mark`) VALUES (NULL, '"+id_of_ cce_sa_fa+"', 'Activity 1', '0');

How can I insert all the ids from the SELECT query to INSERT into cce_activity

Best Answer

if columns same:

INSERT INTO cce_activity select * from cce_sa_fa where id NOT IN (select safa from cce_activity)

if not, something like:

INSERT INTO cce_activity (col1, col2, col3) select col1, col2, col3 from cce_sa_fa where id NOT IN (select safa from cce_activity)