MYSQL Check If some value is exist as value in a field of table
I have table A with field :
id name value
1 item1 822
2 item2 658
3 item3 321
I'm trying to figure out this :
I want to count how many times values is exist in a field
example :
If I'm searching 2 and 8, then
The Result is : The count is 3 for row id 1 ( because it's has two 2 and one 8 )
And the count is 1 for row id 2 ( because it's has one 8 number )
And the count is 1 for row id 3 ( because it's has one 2 number )
select id, case when value like '%2%' then 1 else 0 end +
case when value like '%8%' then 1 else 0 end
from tablename
You can do this with replace()
and length()
(or char_length()
):
select (length(value) - length(replace(replace(value, '2', ''), '8', ''))) as num_occurrences
from t;
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.