I'm having two tables namely ds_message and ds_params, the first table contains the template and the second tables contains the key value pair
Table Structure: ds_message
_____________________________________________
id template
_____________________________________________
1 'Dear {a}, the price of {b} is {c}'
2 'Dear {i}, you selected the product {j}'
Schema:
CREATE TABLE `ds_message` (
`id` int NOT NULL,
`template` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='';
ALTER TABLE `ds_message`
ADD PRIMARY KEY (`id`);
INSERT INTO `ds_message` (`id`, `template`) VALUES
(1, 'Dear {a}, the price of {b} is {c}');
INSERT INTO `ds_message` (`id`, `template`) VALUES
(2, 'Dear {i}, you selected the product {j}');
Table Structure: ds_params
_________________________________________________
id message_id json_key json_value
_________________________________________________
1 1 a John
2 1 b bat
3 1 c $10
4 2 i Emma
5 2 j Jam
Schema:
CREATE TABLE `ds_params` (
`id` int NOT NULL,
`message_id` int NOT NULL,
`json_key` varchar(500) NOT NULL,
`json_value` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='';
ALTER TABLE `ds_params`
ADD PRIMARY KEY (`id`);
INSERT INTO `ds_params` (`id`, `message_id`, `json_key`, `json_value`) VALUES
(1, 1, 'a', 'John');
INSERT INTO `ds_params` (`id`, `message_id`, `json_key`, `json_value`) VALUES
(2, 1, 'b', 'bat');
INSERT INTO `ds_params` (`id`, `message_id`, `json_key`, `json_value`) VALUES
(3, 1, 'c', '$10');
INSERT INTO `ds_params` (`id`, `message_id`, `json_key`, `json_value`) VALUES
(4, 2, 'i', 'Emma');
INSERT INTO `ds_params` (`id`, `message_id`, `json_key`, `json_value`) VALUES
(5, 2, 'j', 'Jam');
I need to replace the keys (for example {a} => John
) in the ds_message
table.
I tried the following code,
UPDATE ds_message AS t
INNER JOIN ds_params m ON m.message_id = t.id
SET t.template = REPLACE(t.template, CONCAT('{', m.json_key , '}'), m.json_value);
Once I executed the code I'm getting the output like this, only the first key gets replaced, remaining keys not get update.
_____________________________________________
id template
_____________________________________________
1 Dear John, the price of {b} is {c}
2 Dear Emma, you selected the product {j}
Kindly assist me how to do this.