Kirk Strobeck Kirk Strobeck - 6 months ago 8
SQL Question

How do I do an insert with a join

I want to take the result of this

SELECT * FROM `orgs`
WHERE `slug` = 'foo'


And use it with this

INSERT INTO `projects-permissions`
(`id`, `project`, `org`)
VALUES
(NULL, 'foo', ORG.ID)

Answer
INSERT INTO `projects-permissions` (`id`, `project`, `org`) 
SELECT NULL, 'foo', ID 
FROM `orgs`
WHERE `slug` = 'foo'

or a little shorter

INSERT INTO `projects-permissions` (`project`, `org`) 
SELECT slug, ID 
FROM `orgs`
WHERE `slug` = 'foo'
Comments