0
I have the following query below, and the idea is to take the value of the @subcategories variable and put in the query’s IN:
set @subcategorias = replace('10, 11, 29, 30, 31', '''', '');
SELECT
car.NomeCaracteristica,
MAX(catcar.Valor) AS Valor,
catcar.Condicional AS Condicional
FROM
caracteristica car
inner join categoriacaracteristica catcar on car.IdCaracteristica = catcar.IdCaracteristica
WHERE
catcar.IdCategoria IN (@subcategorias) AND
catcar.IdCaracteristica = 19 AND
catcar.Valor is not null
GROUP BY
car.NomeCaracteristica,
catcar.Condicional;
Doing so does not return me the amount of records I need. It should return two instead of one.
I know this because if I put the value in the IN in hand (catcar.Idcategoria IN ('10, 11, 29, 30, 31')) it works.
Anybody can tell me what might be going on?
But I did replace the variable (set subcategories = replace('10, 11, 29, 30, 31', ''''', '');) that goes in the IN and out of them, would be the 5 items, but even so, it didn’t work. I even gave a SELECT subcategory to see if it was without the quotes and it was.
– cmcampos86
Yes, I read this in the question. And there is no basis to think that replace has any relation to the number of items. A string is a string.
– Bacco
Blza... understood! I used FIND_IN_SET and it worked. Thanks for the help!
– cmcampos86
@Cmfields86 Note: If you are using a paid language to generate/use queries, you can use IN, but you need to remove the quotes before SQL. I published a solution for pure Mysql, because that’s what I had in the question. But in a language that can manipulate the query (instead of the query values), you can do what you tried (but outside the SQL layer). O
FIND_IN_SET
does string search, but is not as efficient as IN + list.– Bacco
Haaaaa understood @Bacco! I’ll follow this tip! Thanks!
– cmcampos86