I'm new to SQL and I'm struggling with the insert into function. I'm using phpMyAdmin.
I'm trying to take a result of a query, a list (1 column) and add it to a table with more than 1 column with each result being a new row.
For context, I'm working with Olympics data. I have a query that returns a list of countries that had athletes that competed but did not win any medals (country not found in "total medal" table).
-- Find Countries who entered and did not win medals
SELECT find.Country
FROM (
SELECT DISTINCT Country
FROM `athletes`
) AS find
LEFT JOIN (
SELECT DISTINCT Country
FROM `total medals`
) AS inside
ON find.Country = inside.Country
WHERE inside.Country IS NULL;
I am trying to add those 20ish countries to the 'total medals' table. My assumption is that they would be added in with NULLS for every other column.
Country Gold Silver Bronze Total Rank by Gold Rank by Total
[result1] NULL NULL NULL NULL NULL NULL
[result2] NULL NULL NULL NULL NULL NULL
[...etc] NULL NULL NULL NULL NULL NULL
The end goal would have the medal counts be changed to 0s and the Rank by Gold would be 87 and Rank by a Total of 78 for each row. I'm not sure if there would be a way to do that in one step as well, If not, I'm sure I could figure out changing a NULL to something else with.
[result1] & 0 & 0 & 0 & 0 & 87 & 78
[result2] & 0 & 0 & 0 & 0 & 87 & 78
[...etc] & 0 & 0 & 0 & 0 & 87 & 78
There's a very similar question here that almost got me there, but it's missing the query result.
My understanding of this is that I should be able to plug my previous query into the values section.
INSERT INTO `total medals`(Country)
VALUES ([Query]);
But when I try that, I get a syntax error in line 4 (Select find.Country).
-- Find Countries who entered and did not win medals and add them to the medals table
INSERT INTO `total medals`(Country)
VALUES(
SELECT find.Country
FROM (
SELECT DISTINCT Country
FROM `athletes`
) AS find
LEFT JOIN (
SELECT DISTINCT Country
FROM `total medals`
) AS inside
ON find.Country = inside.Country
WHERE inside.Country IS NULL
);
In conclusion:
- How do I plug a query into a table with a different width 2 Extra credit - can you also autofill the 0s 87, and 78 in one step?