Home > Back-end >  How to insert multiple values into different rows using MySQL?
How to insert multiple values into different rows using MySQL?

Time:08-02

I have a table MG_DEVICE_GROUP_IN_GEOZONE that has two columns

| deviceGroup_id| geozone_id|
| ------------- | --------- |

I want to insert multiple values to both columns:

  • in geozone_id should be all ids from nested query
  • in deviceGroup_id should be one value for all - 4525

I'm tried to use this query:

insert into MG_DEVICE_GROUP_IN_GEOZONE (deviceGroup_id, geozone_id)
values (4525, (select id from MG_GEOZONE where account_id = 114 and zoneType in (0, 1 , 3)));

But I receiving error - "[21000][1242] Subquery returns more than 1 row"

I understand why this error appeared but I can't find the right query to do this. Please help me. Thanks

CodePudding user response:

insert into MG_DEVICE_GROUP_IN_GEOZONE (deviceGroup_id, geozone_id)
select 4525,id from MG_GEOZONE where account_id = 114 and zoneType in (0, 1 , 3);

The INSERT statement can be followed by a SELECT statement, which produces the values to be inserted.

  • Related