Kirk Strobeck
Kirk Strobeck

Reputation: 18579

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)

Upvotes: 0

Views: 30

Answers (3)

Aleksandar Đokić
Aleksandar Đokić

Reputation: 2333

INSERT INTO projects-permissions (id, project, org) SELECT null, slug, org FROM orgs WHERE slug = "foo";

Upvotes: 1

Walter_Ritzel
Walter_Ritzel

Reputation: 1397

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

Upvotes: 1

juergen d
juergen d

Reputation: 204756

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'

Upvotes: 1

Related Questions