{"id":1263,"date":"2012-06-30T16:08:26","date_gmt":"2012-06-30T23:08:26","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1263"},"modified":"2015-04-07T08:49:19","modified_gmt":"2015-04-07T15:49:19","slug":"making-mysql-do-what-you-want","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1263","title":{"rendered":"Making MySQL do what you want."},"content":{"rendered":"<p>Just some miscellaneous code that I\u2019ve been using to update fields in my database. Basically lots of trimming and concatenating. A little substring manipulating thrown in for good measure.<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM `words_for_slps` WHERE `F` REGEXP &#039;z$&#039; AND `pronunciation` REGEXP &#039;z$&#039;\n\nUPDATE`words_for_slps` SET `phonemes` = TRIM(TRAILING &#039;s&#039; FROM phonemes) WHERE `F` REGEXP &#039;z$&#039; AND `phonemes` REGEXP &#039;s$&#039; AND `word` = &#039;Ares&#039;\n\nUPDATE`words_for_slps` SET `phonemes` = CONCAT(phonemes,&#039;z&#039;) WHERE `F` REGEXP &#039;z$&#039; AND `pronunciation` REGEXP &#039;z$&#039;\n\nUPDATE`words_for_slps` SET `phonemes` = TRIM(TRAILING &#039;s hz&#039; FROM phonemes) WHERE `F` REGEXP &#039;hz$&#039; AND `phonemes` REGEXP &#039;hz$&#039;\n\nUPDATE`words_for_slps` SET `phonemes` = CONCAT(phonemes,&#039;\u0283 \u025b z&#039;) WHERE `F` REGEXP &#039;z$&#039;\n\nUPDATE`words_for_slps` SET `phonemes` = CONCAT(phonemes,&#039;z&#039;) WHERE `F` REGEXP &#039;z$&#039; AND `pronunciation` REGEXP &#039;z$&#039;\nUPDATE`words_for_slps` SET `phonemes` = CONCAT(phonemes,&#039; \u025b s&#039;) WHERE `F` REGEXP &#039;s&#039; AND `phonemes` REGEXP &#039;n$&#039;\n\nUPDATE `words_for_slps` SET `phonemes` = TRIM(LEADING &#039;c&#039; FROM phonemes) WHERE `word` REGEXP &#039;^c&#039; AND `I` LIKE &#039;c&#039;\nUPDATE `words_for_slps` SET `phonemes` = CONCAT(&#039;k&#039;,phonemes) WHERE `phonemes` REGEXP &#039;^ &#039; AND `I` LIKE &#039;c&#039;\n\nUPDATE `words_for_slps` SET `phonemes` = TRIM(LEADING &#039;k&#039; FROM phonemes) WHERE `word` REGEXP &#039;^cy&#039; AND `phonemes` REGEXP &#039;^k&#039;\nUPDATE `words_for_slps` SET `phonemes` = CONCAT(&#039;s&#039;,phonemes) WHERE `word` REGEXP &#039;^cy&#039; AND `phonemes` REGEXP &#039;^ &#039;\n\nUPDATE `words_for_slps` SET `phonemes` = REPLACE(phonemes, &#039;t \u025b,i&nbsp;&nbsp;d&#039;, &#039;t \u0259 d&#039;) WHERE `phonemes` REGEXP &#039;t \u025b,i&nbsp;&nbsp;d$&#039; AND `pronunciation` REGEXP &#039;t u d$&#039;\n\nUPDATE `words_for_slps` SET `phonemes` = REPLACE(phonemes, &#039;n z&#039;, &#039;nz&#039;) WHERE `pronunciation` REGEXP &#039;n z$&#039; AND `phonemes`REGEXP &#039;n z$&#039;\n<\/code><\/pre><\/p>\n<p>I used these select statements to look for words that need some manual cleanup.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM `words_for_slps` WHERE `F` &lt;&gt; SUBSTRING_INDEX(phonemes, &#039; &#039;, -1)\nSELECT *&nbsp;&nbsp;FROM `words_for_slps` WHERE `word` REGEXP &#039;^w&#039; AND `pronunciation` NOT REGEXP &#039;^w&#039; AND `pronunciation` IS NOT NULL\n<\/code><\/pre><\/p>\n<p>This works when I want to update the fields in one table with values from fields in another table.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nUPDATE words_for_slps, words_for_slpsBAK12\nSET words_for_slps.grade = words_for_slpsBAK12.grade\nWHERE words_for_slps.word_id = words_for_slpsBAK12.word_id;\n<\/code><\/pre><\/p>\n<p>Even though I\u2019m only updating one table you\u2019d think this should work, but it doesn&#8217;t.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nUPDATE `words_for_slps` \nLEFT JOIN `words_for_slpsBAK12` ON `words_for_slps.word_id` = `words_for_slpsBAK12.word_id`\nSET `words_for_slps.grade` = `words_for_slpsBAK12.grade`\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just some miscellaneous code that I\u2019ve been using to update fields in my database. Basically lots of trimming and concatenating. A little substring manipulating thrown in for good measure. SELECT *&nbsp;&nbsp;FROM `words_for_slps` WHERE `F` REGEXP &#039;z$&#039; AND `pronunciation` REGEXP &#039;z$&#039; UPDATE`words_for_slps` SET `phonemes` = TRIM(TRAILING &#039;s&#039; FROM phonemes) WHERE `F` REGEXP &#039;z$&#039; AND `phonemes` REGEXP &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1263\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Making MySQL do what you want.<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-1263","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1263"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1263\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}