boisterouslobster
boisterouslobster

Reputation: 1293

MYSQL Using Subquery Result in Insert

I some data that I would like to insert into another table. In particular, I'm getting some ID values from the table guest_list_parties, and would like to pass those into the table guest_list_parties_secondary.

The party_id is what is being returned by the subquery; 2031 is the secondary_event_id I would like to pass along with every record.

However, MYSQL reports with an error, stating: 'Subquery returns more than 1 row'

My code:

INSERT INTO guest_list_parties_secondary (party_id, secondary_event_id)

VALUES((SELECT id
FROM guest_list_parties
WHERE event_id = 31919),2031)   

Any help/direction is greatly appreciated.

Upvotes: 1

Views: 26

Answers (1)

Hunter McMillen
Hunter McMillen

Reputation: 61512

Your insert statement expects two columns to be supplied: party_id and secondary_event_id. VALUES() expects literal values, you should just explicitly select 2031 in your query:

INSERT INTO guest_list_parties_secondary (party_id, secondary_event_id)
SELECT id, 2031
FROM guest_list_parties
WHERE event_id = 31919;

Upvotes: 2

Related Questions