I’m trying to update an email address value from users first- and lastname within the same table.
Pattern for generated Mail: <Firstname>.<Lastname>@test.org
Example Table “Persons”:
Firstname Lastname email
Henley Figueroa [email protected]
Samina Morrison [email protected]
Dev Rowe [email protected]
Wished result for table “Persons”:
Firstname Lastname email
Henley Figueroa [email protected]
Samina Morrison [email protected]
Dev Rowe [email protected]
SQL Code
UPDATE Persons
SET
email = (SELECT FirstName || "." || LastName ||"@"||"test.org" FROM Persons)
Actual result for table “Persons”:
Firstname Lastname email
Henley Figueroa [email protected]
Samina Morrison [email protected]
Dev Rowe [email protected]
Only the first record of the returned result table is used over and over. Why?
If I ommit the SELECT:
UPDATE Persons
SET
email = FirstName || "." || LastName ||"@"||"test.org"
I get the expected result.
Referring to your answers, I'm extending my question with an example of two tables. One with only names, the second for mail addresses.
Table Persons:
| ID* | Firstname | Lastname |
|---|---|---|
| 1 | Henley | Figueroa |
| 2 | Samina | Morrison |
| 3 | Dev | Rowe |
Table Addresses:
| ID* | |
|---|---|
| 1 | [email protected] |
| 2 | wrong@wrong,ng |
| 3 | [email protected] |
UPDATE Addresses
SET email = (SELECT Firstname ||"."|| Lastname || "@test.org"
FROM Persons WHERE Persons.id = Addresses.id)
Here the UPDATE with SET and SELECT works (Every person gets a unique mail address). Shouldn’t I get the same problem here? From the output of SELECT I see that I get again (as expected) three records.