Search code examples
mysqlsqlinsertsubquery

SQL subquery in INSERT?


Is it possible to use a subquery in an INSERT statement?

INSERT INTO table (age, p_id) 
VALUES('22', '(SELECT id FROM people WHERE name='Bob')')

Is there a working form of what I'm attempting?

Using MySQL database


Solution

  • INSERT INTO MyTable (age, p_id)
    SELECT '22', ( select id from people where name = 'bob')
    

    or you could do this

    INSERT INTO MyTable (age, p_id)
    SELECT '22', id 
    FROM people
    WHERE name = 'bob'