0

I'm trying to use the MYSQL statement IN to select only the data I need from my table. For example: I have a field that save categories by ID separated by comma (7,1,10,25,20). If I use this code:

SELECT * FROM cancelamento WHERE ( motivos_int_id IN ( 7 ) )

I get only the rows that start with the number 7. If the data from that row is 8,7,14, for example, it doesn't return any data. Even if I change the code to:

SELECT * FROM cancelamento WHERE ( motivos_int_id IN ( 7,8 ) )

Am I doing something wrong? I need to fetch all the data that has the number 7, for example, regardless of the order of the number 7.

MeNa
  • 1,467
  • 9
  • 23
Foreba
  • 410
  • 4
  • 15

2 Answers2

1

you can use FIND_IN_SET

    SELECT * FROM cancelamento WHERE find_in_set(7 , motivos_int_id)

a little example here

echo_Me
  • 37,078
  • 5
  • 58
  • 78
0

First print your motivos_int_id separate , check your id'structure it could be like this one (12,3,4,) in last there is comma appended , so if this scenerio happend use rtrim() function in php and trim your right side comma and your query is correct .

select * from tbl_name where id IN(1,2,3)