INSERT INTO Table from multiple tables

前端 未结 3 496
忘了有多久
忘了有多久 2020-12-05 05:35

Hey so I have a Junction table linking two unrelated tables. Both the tables have ID\'s. I need to select the ID from each table using WHERE<

相关标签:
3条回答
  • 2020-12-05 05:42

    Try this query:

         INSERT INTO C (aID, bID) 
         SELECT A.ID, B.ID 
         FROM A, B 
         WHERE A.Name='Me'
         AND B.Class='Math';
    
    0 讨论(0)
  • 2020-12-05 06:02

    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';
    
    0 讨论(0)
  • 2020-12-05 06:02

    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

    0 讨论(0)
提交回复
热议问题