Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Count values from comma separated field in mysql

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.04k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: