Sometimes in mysql we have to count the values that are comma separated from a field in mysql. For this we will use the length function in mysql as follows:
select LENGTH(fieldname)-LENGTH( REPLACE( fieldname , ',', '' ) )+1 from tablename where TRIM( IFNULL( fieldname , '' ) ) > ''
Here, first we will count the number of characters in a field including comma then we will subtract it from the number of characters in a field excluding comma i.e., we will get number of comma from this which always be 1 less then the number of words in the field. That's why we had added 1 to the field.
Now next we have given the condition here TRIM( IFNULL( fieldname , '' ) ) > '' which check whether the field is null or not.
0 Comment(s)