I did the following, I make the consultation using group_concat
of the values I wish to replace others' to get them concatenated separated by commas. I use the Replace
and Substring_index
for, at each substitution, the value that has already been used is excluded, to determine the next value to be obtained using Substring_index
again.
It’s working, maybe it’s not the proper method, because if there is
more than 20 values I wish to replace gives error, but up to 20 works.
Follows sqlfiddle:
http://sqlfiddle.com/#! 9/6f8f1f/16/0
-- borrowed from https://stackoverflow.com/q/7745609/808921
CREATE TABLE IF NOT EXISTS `mdl_question` (
`id` int(6) unsigned NOT NULL,
`questiontext` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `mdl_question` (`id`, `questiontext`) VALUES
('1', '<img src="@@PLUGINFILE@@/imagem1.svg"> <img src="@@PLUGINFILE@@/imagem2.svg"> <img src="@@PLUGINFILE@@/imagem3.svg">');
CREATE TABLE IF NOT EXISTS `mdl_files` (
`id` int(6) unsigned NOT NULL,
`contenthash` varchar(200) NOT NULL,
`filename` varchar(200) NOT NULL,
`component` varchar(200) NOT NULL,
`itemid` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `mdl_files` (`id`, `contenthash`, `filename`, `component`, `itemid`) VALUES
('1','8ca79795dc7c3c7fc836faac6d0d7422627486cb','imagem1.svg','question', '1'),
('2','418679c9ecac4eb42452d4c51332710d24f6de8a','imagem2.svg','question', '1'),
('3','418679c9ecac4eb42452d4c51332710d24f6de8a','imagem3.svg','question', '1')
SELECT
Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(questiontext,
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-20),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-20),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-19),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-19),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-18),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-18),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-17),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-17),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-16),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-16),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-15),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-15),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-14),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-14),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-13),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-13),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-12),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-12),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-11),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-11),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-10),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-10),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-9),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-9),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-8),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-8),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-7),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-7),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-6),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-6),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-5),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-5),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-4),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-4),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-3),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-3),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-2),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-2),',',1)),
concat('@@PLUGINFILE@@/',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(filename),',',-1),',',1)), SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash)),',',-1),',',1))
AS questiontext
FROM mdl_question q
inner join mdl_files f on f.itemid=q.id
This is happening because its variable
@grupoEndereco
is returningnull
.– Roberto de Campos
@Robertodefields in the fiddle example this is @groupEnderco output:
8c/a7/8ca79795dc7c3c7fc836faac6d0d7422627486cb,8c/a7/8ca79795dc7c3c7fc836faac6d0d7422627486cb
– Miguel Silva
two addresses separated by comma
– Miguel Silva
Does so:
SELECT
@enderecoHashImagem := concat(substr(contenthash,1,2),'/',substr(contenthash,3,2),'/',contenthash) as enderecoHashImagem,
@nomeImagem := filename as nomeImagem, @grupoEndereco := GROUP_CONCAT(@enderecoHashImagem) AS grupoEndereco,
@grupoEndereco, Replace(questiontext, concat('@@PLUGINFILE@@/',@nomeImagem), @grupoEndereco) AS questiontext

FROM mdl_question q 

inner join mdl_files f on f.itemid=q.id

where component = 'question'
you will see that the second time you use this variable will returnnull
.– Roberto de Campos
@Robertodecampos vdd, the second returns null
– Miguel Silva
Is it possible to do what I want, use replace with the values of this variable ?
– Miguel Silva
@Robertodecampos now I’m not getting anymore
null
, but the result is not yet as expected, if you can take a look, thank you– Miguel Silva
To tell you the truth, I’ve never used variables this way, what I usually do is duplicate the code, I’ll put as an answer for you to understand better.
– Roberto de Campos