I have a mysql table that has 2 columns – Column 1 contains a string value, and Column 2 contains the number of times that string value occurred.
I’m trying to find the string abc.X.def, where the beginning of the string is “abc.”, followed by one or more characters, then the string “.def”. There could be more characters following “.def”.
How can I find such strings, then add the occurrence of such strings and display the results?
For example, if I have
abc.111.def23 1
abc.111.def 2
abc.22.def444 1
abc.111.def 1
Then I will get
abc.111.def23 1
abc.111.def 3
abc.22.def444 1
Thank you.
I think you can use
LIKEandSUM:Note the
GROUP BYclause, since your table doesn’t seem to have a primary or unique key on StringValue. Also, note the second%after ‘def’, since your string values don’t end with ‘.def’ but can actually have some digits after that, it would seem. For that, if your free characters are always decimal digits ([0-9]), you might try something like one of theseWHEREclauses in stead:They’re equivalent, and require some digits between the dots/periods (that’s what the plus does), and allow digits after the ‘def’ (that’s what the asterisk does). The periods themselves need to be escaped with backslash, since otherwise they represent any character. The
^and$tellREGEXPto match the complete string, from beginning to end. Lastly,\dis shorthand for decimal digits, which can also be represented by the character range[0-9].