INSERT INTO Table from multiple tables

Assuming there is only one value in each table for the given Name and Class, the easiest method is just to enclose your subqueries in ():

INSERT INTO c VALUES (
(SELECT ID from a where Name='Me'),
(SELECT ID from b where Class ='Math')
)

Demo on dbfiddle


Try this query:

     INSERT INTO C (aID, bID) 
     SELECT A.ID, B.ID 
     FROM A, B 
     WHERE A.Name='Me'
     AND B.Class='Math';

Another way can be

INSERT INTO c (aID, bID)
SELECT 
   (SELECT A.id FROM TableA A WHERE A.names = 'sometext'), 
   B.id FROM TableB B 
WHERE 
   B.x_name ='othertext';

Tags:

Mysql

Sql