AnthonySterling Posted February 6, 2008 Share Posted February 6, 2008 Hey all, Does anyone know how I can count the number of times a string or character appears in a certain field in MySQL? For example.. ABC$DEF$GHIJ Where the returning result would be 2 for the character '$'. I'm guessing the SQL would be something similar to..... SELECT id,content FROM articles COUNT(Something?) AS stringCount I'm looking for a SQL result with the ID number and string count, make sense? :blush: I'm flummoxed.... Cheers again, SilverB. :huh: Link to comment Share on other sites More sharing options...
0 Mike Posted February 6, 2008 Share Posted February 6, 2008 dont think there is, would be easy enough to do in PHP/ASP/etc though Link to comment Share on other sites More sharing options...
0 AnthonySterling Posted February 6, 2008 Author Share Posted February 6, 2008 Bugger, surely something so useful must be part of the basic package. Link to comment Share on other sites More sharing options...
0 AnthonySterling Posted February 6, 2008 Author Share Posted February 6, 2008 Anyone else hazard a guess? :rolleyes: Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted February 6, 2008 Share Posted February 6, 2008 Anyone else hazard a guess? :rolleyes: Why guess? Go to http://dev.mysql.com/doc/ and research it. If MySQL supports user-defined functions, you could write your own. Link to comment Share on other sites More sharing options...
0 AnthonySterling Posted February 6, 2008 Author Share Posted February 6, 2008 Yeah.... I did look around the manual, mainly at the string functions list, I couldn't see anything there. However, there was something using MATCH but it seems my DB engine does not support it. Hence, my question as to why maybe a generic function which I am missing. Now, creating a function sounds interesting, I never knew you could do that, sounds a little involved, but hey, in for a penny.... If somebody does have any further information I'd be most grateful, thanks, SilverB. Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted February 6, 2008 Share Posted February 6, 2008 Yeah....I did look around the manual, mainly at the string functions list, I couldn't see anything there. However, there was something using MATCH but it seems my DB engine does not support it. Hence, my question as to why maybe a generic function which I am missing. Now, creating a function sounds interesting, I never knew you could do that, sounds a little involved, but hey, in for a penny.... If somebody does have any further information I'd be most grateful, thanks, SilverB. I'm no MySQL guy, but from what I can see, MySQL does not support user defined functions. Your best bet is to just return your data and utilize code to extract the info you want. Link to comment Share on other sites More sharing options...
0 conradescape Posted April 17, 2009 Share Posted April 17, 2009 not professional, but does the job : mysql> SELECT LENGTH('a$b$c$')-LENGTH(REPLACE('a$b$c$','$','')) AS test; +------+ | test | +------+ | 3 | +------+ 1 row in set (0.00 sec) Link to comment Share on other sites More sharing options...
Question
AnthonySterling
Hey all,
Does anyone know how I can count the number of times a string or character appears in a certain field in MySQL?
For example..
Where the returning result would be 2 for the character '$'.
I'm guessing the SQL would be something similar to.....
I'm looking for a SQL result with the ID number and string count, make sense? :blush:
I'm flummoxed....
Cheers again,
SilverB. :huh:
Link to comment
Share on other sites
7 answers to this question
Recommended Posts