'SQL join multiple values on same column row
My problem is as follows.
I have a database with certain Healthcare services. These services can have 1 or more emails that are stored in HealthcareServiceEmail with a foreign key referencing HealthcareService. I would like to show these emails in the Healthcareservice TABLE.
For this I would need to insert the values from HealthcareserviceEmail INTO the table Healthcareservice where their foreign key matches the key from Healthcareservice.
Because there can be multiple emails I would need to insert multiple values in the same cell. I would also need to insert them into already existing rows from healthcareservice (If I understand correctly). What I have for now is this:
CREATE TABLE IF NOT EXISTS ADMINISTRATION.HealthcareServiceEmail (
id BIGINT NOT NULL,
email VARCHAR(255),
healthcareService_id BIGINT,
name VARCHAR(255),
uuid VARCHAR(255),
PRIMARY KEY (id)
);
ALTER TABLE ADMINISTRATION.HealthcareServiceEmail
ADD FOREIGN KEY (healthcareService_id)
REFERENCES ADMINISTRATION.HealthcareService (id) DEFERRABLE;
ALTER TABLE ADMINISTRATION.HealthcareService
ADD COLUMN IF NOT EXISTS email VARCHAR(255);
INSERT INTO ADMINISTRATION.HealthcareService (id,email)
SELECT HealthcareServiceEmail.healthcareService_id,HealthcareServiceEmail.email from ADMINISTRATION.HealthcareServiceEmail
INNER JOIN ADMINISTRATION.HealthcareService ON ADMINISTRATION.HealthcareServiceEmail.healthcareService_id = ADMINISTRATION.HealthcareService.id
Now this obviously does not work, but I am not fully sure what to do to solve this as I have looked into SQL but it is definitely not my strong suit.
Right now I am trying to enter the Emails to show up at the right Service. I tried joining on the ID and inserting that into the email field for HealthcareService but my table does not fill up.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
