I have got a football world cup database. These are the two important table I'm working on right now:
CREATE TABLE Countries(
Cid SERIAL PRIMARY KEY,
Name VARCHAR(256) NOT NULL UNIQUE
);
CREATE TABLE Clubs(
Ncid SERIAL PRIMARY KEY,
Name VARCHAR(256) NOT NULL UNIQUE,
Cid INT REFERENCES Countries NOT NULL
);
So I added a column 'country' in the clubs table which represents the country the club is in. The problem now is I need to populate the cid
column of the clubs table with the cid
that matches the country in the countries table.
I have tried this and it didn't work:
ALTER TABLE clubs ADD COLUMN country VARCHAR(255)
INSERT INTO clubs(name, country)
SELECT DISTINCT club_name, club_country
FROM tempsquads; //this loads the data from a temporary tabe into my table
INSERT INTO clubs(cid)
SELECT cid
FROM countries
WHERE clubs.country = countries.name;
Has anyone an idea of how I can perform such a query?
In your INSERT, link tempsquads to country:
INSERT INTO CLUBS(name, cid)
(SELECT t.club_name, c.cid
FROM tempsquads t
INNER JOIN COUNTRIES c ON t.club_country=c.name)
Don't add the country name field to the clubs table--you will link on id when you need the name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.