Here is a simplified version of what I'm trying to do, which shows the problem best. My database looks like this:
users table:
| user_id | first_name |
|---|---|
| 1 | Bob |
| 2 | Dave |
| 3 | Steven |
settings table:
| name | value |
|---|---|
| format_string | Hello {first_name}! |
Now I want to retrieve the format_string with inserted user data for every user. If I hardcode the my format_string into my SQL like this, it works:
SELECT first_name,
REPLACE(
"Hello {first_name}!",
"{first_name}",
first_name
)
AS greeting
FROM users
I get this output, which is expected:
| first_name | greeting |
|---|---|
| Bob | Hello Bob! |
| Dave | Hello Dave! |
| Steven | Hello Steven! |
But if I use the format_string from my settings table, like this:
SELECT first_name,
REPLACE(
(SELECT value FROM settings WHERE name = "format_string"),
"{first_name}",
first_name
)
AS greeting
FROM users
I get this output, which is absolutely not expected:
| first_name | greeting |
|---|---|
| Bob | Hello Bob! |
| Dave | Hello Bob! |
| Steven | Hello Bob! |
Does anyone know what the problem there is and how to fix it?