Home > Software engineering >  How to get the exact value from sql like query
How to get the exact value from sql like query

Time:03-22

I have a Mysql database that contains some category ids on it which stores comma-separated values on a table.

sql table view

By using select * from style where categories like '%8,%'; it returns all the values end with 8. For example, if the table rows have two values like 8 and 148 it returns both rows. But I want to get only the rows that contain 8. How to do it

CodePudding user response:

Storing multiple values in a single column is a denormalised design that will almost always cause you problems. However you need to add commas to both sides and compare:

select *
from Style
where concat(',',Categories,',') like '%,8,%';

CodePudding user response:

I think what is bothering you is the comma, from what I understand you want to count the 8, so this would be fine.

select * from style where categories like '%,8,%';

CodePudding user response:

You could try something like this:

SELECT * FROM style 
WHERE categories LIKE '%8%';
  • Related