Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to insert multiple values into different rows using MySQL?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading