I select data with this query
SELECT * FROM `tbl_post` WHERE cate IN (15)
But only records can be found that The number 15 is the first like : "15,16,18" , "15" can not be found If the number is 15 in the middle or last like : "14,15,16"
I select data with this query
SELECT * FROM `tbl_post` WHERE cate IN (15)
But only records can be found that The number 15 is the first like : "15,16,18" , "15" can not be found If the number is 15 in the middle or last like : "14,15,16"
MySQL has a specific string function called find_in_set()
for the purpose of searching for a value in a comma-separated string:
select * from tbl_post where find_in_set('15', cat) > 0
While this will work for your specific use-case, let me pinpoint that storing delimited list in relational databases is not a good practice, and should be generally avoided. You should have a separate table to store cate
s, which each value on a separate row.
Recommended reading: Is storing a delimited list in a database column really that bad?
Simply using "like" might help -
select * from tbl_post where cate like '15,%' or cate like '%,15,%' or cate
like '%15';
cate like '15,%' : Will get the 15 placed at the beginning
cate like '%,15,%' : Will get the 15 placed in the middle
cate like '%15' : Will get the 15 placed at the end