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 values into a table from the result of a SELECT statement SQL

I’ve got two tables, one called PLAYERS with this structure:

PLAYERS (name, team, goals, cod)

and another one called NEW_PLAYERS with this structure:

NEW_PLAYERS (name, team, goals)

I want to insert values from NEW_PLAYERS into PLAYERS setting the value 1 for the register cod of all of NEW_PLAYERS.

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

I thought this code could work but it doesn’t.

INSERT INTO PLAYERS (name, team, goals, cod) 
VALUES ((SELECT name, team, goals FROM NEW_PLAYERS), 1);

Does anyone have a better option?

>Solution :

You were almost there. Try this.

INSERT INTO PLAYERS(name,team,goals,cod) 
SELECT name,team,goals,1 FROM NEW_PLAYERS
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