I have a table with a row with the data like '/2323/3235/4545/222/'
how can i count the number of / in every row using mysql only.
CHAR_LENGTH() function MySQL CHAR_LENGTH() returns the length (how many characters are there) of a given string. The function simply counts the number characters and ignore whether the character(s) are single-byte or multi-byte.
Counting all of the Rows in a Table. To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.
Introducing the LENGTH() and REPLACE() Functions The LENGTH() function returns the length of a string in bytes. This has some important ramifications because it means that for a string containing five 2-byte characters, LENGTH() returns 10. To count straight characters, use CHAR_LENGTH() instead.
In MySQL, the COUNT(DISTINCT expression) method is used to sum non-Null values and distinct values of the column 'expression'. To count a distinct number of non-null values in the column 'Age' we have been using the below query. You will find 6 non-null and distinct records of column 'Age' from the table 'social'.
A quick search came up with this:
SELECT LENGTH('/2323/3235/4545/222/') - LENGTH(REPLACE('/2323/3235/4545/222/','/','')) ... etc;
link
SELECT LENGTH('/2323/3235/4545/222/') - LENGTH(REPLACE('/2323/3235/4545/222/', '/', '')) AS `occurrences`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With