Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to count no of specific symbol in a row in mysql

Tags:

mysql

count

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.

like image 496
Ankit Sharma Avatar asked May 11 '12 08:05

Ankit Sharma


People also ask

How do I count characters in MySQL?

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.

How do I count specific rows in MySQL?

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.

How do you count occurrences of a character in a string in MySQL?

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.

How do I count the number of particular column values in MySQL?

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'.


2 Answers

A quick search came up with this:

SELECT LENGTH('/2323/3235/4545/222/') - LENGTH(REPLACE('/2323/3235/4545/222/','/','')) ... etc;
like image 154
JK. Avatar answered Sep 20 '22 11:09

JK.


link

SELECT LENGTH('/2323/3235/4545/222/') - LENGTH(REPLACE('/2323/3235/4545/222/', '/', '')) AS `occurrences`
like image 33
M.I.T. Avatar answered Sep 19 '22 11:09

M.I.T.