Mysql - Reading data in a string

1

There is a Cost Center table called "cost" in it, it contains IDs (field "code") that you would like returned in a SELECT. The however is that the data needs to be compared to a comma-separated string, which are the values that I need to select.

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?

    
asked by anonymous 25.02.2015 / 22:21

1 answer

1

You should separate the items in the IN clause, like this:

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

Or:

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

See example working on ideone

    
25.02.2015 / 23:01