More MySQL goodness

It can be tricky to update records in a MySQL database using regular expressions because the stock install doesn’t support replacement strings. Occasionally, you get lucky and your data can be manipulated with a combination of REGEXP and strings. This is one case.

My phonemes field is constructed of phonemes separated by spaces. e.g.


b ɛ,i n
bl æ,ə n tʃ
br æ,ə n t æ,ə

I want to update the Initial and Final sounds fields with the sounds IF they are consonants but not if they are vowels. My REGEXP selects all of the consonants—including sh (ʃ) and th (θ or ð) at the beginning ‘^’ or end ‘$’ of the phoneme. Then I split the phonemes field by the first or last space and place that value into the Initial or Final sound field. Along the way I convert it to lower case. Also note that I want initial the initial w to count as a consonant, but not if it occurs at the end—but my phoneme rules should have caught most of those.


UPDATE `words_for_slps` 
SET `I` = LOWER(SUBSTRING_INDEX(phonemes,' ', 1)) 
WHERE `phonemes` REGEXP '^[ʃθðbcdfghjklmnpqrstvwxz]+'


UPDATE `words_for_slps` 
SET `F` = LOWER(SUBSTRING_INDEX(phonemes,' ', -1)) 
WHERE `phonemes` REGEXP '[ŋʃθðbcdfghjklmnpqrstvxz]$'