REPLACE in the IN - Mysql clause

Asked

Viewed 197 times

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?

1 answer

4


Basically it’s a syntax problem.

You said if you do it "in hand" it works:

catcar.IdCategoria IN ('10, 11, 29, 30, 31')

Works only if idcategoria be exactly the string 10, 11, 29, 30, 31, after all the IN expects a comma-separated list of items.

The way you did, '10, 11, 29, 30, 31' is an item only (the fact of having commas inside the string has nothing to do with the list of items IN waiting).

These two examples are completely different from the above line:

catcar.IdCategoria IN ( 10, 11, 29, 30, 31 )

and

catcar.IdCategoria IN ( '10', '11', '29', '30', '31' )

In this case, there are 5 different items, not one string as described in the question.


If you want to find data in a string separated by commas, the function is another:

WHERE FIND_IN_SET( catcar.IdCategoria, '10,11,29,30,31' )

Handbook:

https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set


Note: what could work, in your case, is to apply your reasoning to an extra language that is generating the query, there is a completely different situation.

Example in PHP:

$lista = '10, 11, 29, 30, 31';
$sql = ' ... resto da query... WHERE catcar.IdCategoria IN ('.$lista.') AND ...';
mysqli_query( sql );

This already works because it is happening outside the SQL layer. When SQL processes the string, the quotes would not be there anymore. Then yes, a replace of the respective language would still be effective.

  • 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.

  • 1

    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.

  • Blza... understood! I used FIND_IN_SET and it worked. Thanks for the help!

  • @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.

  • Haaaaa understood @Bacco! I’ll follow this tip! Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.