Mysql - Reading data in a string

Asked

Viewed 50 times

1

There is a Cost Center table called "ccusto", in it, contains Ids ("code" field) that would like to be returned in a SELECT. However, the data needs to be compared to a string with comma separation, which are the values that need to be selected.

Exemplo:
SELECT  codigo
FROM    ccusto
WHERE   codigo  IN('1,2,3');

Returno da consulta:
1

Retorno esperado da consulta:
1
2
3

What operator or function can be used in this case?

  • Hello mcardoso, I don’t know if this is what you’re looking for, but I believe you should change the IN('1,2,3') for IN(1,2,3), I understand that you want to pass a string in IN but the ideal is to use items of the same column type, I believe that code is an integer, in this case the ideal would be to pass the values as integers separated by comma.

1 answer

1

You must separate the items in the clause IN, thus:

IN('1','2','3');

Or:

SELECT codigo
FROM ccusto
WHERE ',1,2,3,' LIKE CONCAT('%,',codigo,',%');

See example working on ideone

Browser other questions tagged

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