简体   繁体   中英

MySQL FIND_IN_SET() not working as expected

I have the following table:

... | parents_id   | ...
________________________
... | 1, 40, 7     | ...
... | 10, 4, 7, 1  | ...
... | 45, 40, 1, 7 | ...
... | other_rows   | ...

Now, I need to get these three rows, I use this query SELECT * FROM products WHERE FIND_IN_SET(1, parents_id) > 0 , but I only get the first row ( 1, 40, 7 ), any help?

As per documentation - FIND_IN_SET 's second argument is a comma separated list. So the value 10, 4, 7, 1 being split by a comma becomes to the following 4 values:

  1. 10
  2. 4 - space followed by 4
  3. 7 - space followed by 7
  4. 1 - space followed by 1

None of them equal to 1

Solution: stop using this approach and normalize your schema to use one-to-many (or many-to-many).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM