Ethan Allen
Ethan Allen

Reputation: 14835

How can I combine this INSERT and SELECT MySQL statement?

This pulls back one row:

SELECT id FROM pin WHERE pinpicsid = '1'

I need the result of that statement to go where this X is:

INSERT INTO user_collection (pinid, username)
VALUES (X, 'ethanwa')

Is there a way to combine these two?

Upvotes: 0

Views: 44

Answers (2)

juergen d
juergen d

Reputation: 204756

INSERT INTO user_collection (pinid, username)
SELECT id, 'ethanwa' 
FROM pin 
WHERE pinpicsid = '1'

Upvotes: 1

Peter van der Wal
Peter van der Wal

Reputation: 11806

Use the INSERT INTO SELECT -statement

INSERT INTO user_collection (pinid, username)
SELECT id, 'ethanwa' FROM pin WHERE pinpicsid = '1'

Upvotes: 4

Related Questions