{"id":232,"date":"2013-10-31T04:57:53","date_gmt":"2013-10-31T04:57:53","guid":{"rendered":"http:\/\/www.advancedwebhelp.com\/blog\/?p=232"},"modified":"2013-11-02T05:09:09","modified_gmt":"2013-11-02T05:09:09","slug":"2-great-mysql-conditionals","status":"publish","type":"post","link":"https:\/\/www.advancedwebhelp.com\/blog\/2013\/2-great-mysql-conditionals","title":{"rendered":"Using REGEXP in mySQL WHERE Conditionals"},"content":{"rendered":"<div id=\"posting\">\n<p><a href=\"http:\/\/www.advancedwebhelp.com\/blog\/wp-content\/uploads\/2013\/11\/logo-mysql-s.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-325 alignright\" style=\"margin-top: -60px;\" alt=\"SQL logo\" src=\"http:\/\/www.advancedwebhelp.com\/blog\/wp-content\/uploads\/2013\/11\/logo-mysql-s.png\" width=\"208\" height=\"208\" srcset=\"https:\/\/www.advancedwebhelp.com\/blog\/wp-content\/uploads\/2013\/11\/logo-mysql-s.png 208w, https:\/\/www.advancedwebhelp.com\/blog\/wp-content\/uploads\/2013\/11\/logo-mysql-s-150x150.png 150w, https:\/\/www.advancedwebhelp.com\/blog\/wp-content\/uploads\/2013\/11\/logo-mysql-s-148x148.png 148w, https:\/\/www.advancedwebhelp.com\/blog\/wp-content\/uploads\/2013\/11\/logo-mysql-s-31x31.png 31w, https:\/\/www.advancedwebhelp.com\/blog\/wp-content\/uploads\/2013\/11\/logo-mysql-s-38x38.png 38w\" sizes=\"auto, (max-width: 208px) 100vw, 208px\" \/><\/a>Recently, I needed a MySQL conditionals that I had not used previously. \u00a0 I needed to add a condition that included an OR condition on a single field.\u00a0 While <strong>OR<\/strong> is available in the WHERE conditional, it along with the LIKE filter overly complicated my query. \u00a0 Some OR conditionals become very complicated with the OR conditional.\u00a0 I found that the MySQL function <strong>REGEXP<\/strong> was just what the doctor ordered.\u00a0\u00a0 It opened up a wide variety of options, and simplified the query.<\/p>\n<p>Using REGEXP\u00a0 turned out to be not only easy, but, convenient. We can use regular expressions.\u00a0 If you are used to using regexp, you will love this feature. For example, assume you have fields called`price` and `view` and you want to find records with the price under $300,000 and a `view` of &#8216;ocean,&#8217; &#8216;river,&#8217; or &#8216;lake,&#8217; you can use regular expressions. You may use the following SQL conditional:<\/p>\n<p style=\"margin-left: 60px; font-weight: bold;\">WHERE `price` &lt; 400000 AND `view` REGEXP &#8216;Ocean|River|Lake&#8217;<\/p>\n<p>As with other places you can use regexp, this allows you to say you would like the field `view` to contain the Ocean, River or Lake &#8230; regardless of whatever else is in that field.\u00a0\u00a0 Therefore, it references the field once and incorporates the concept of LIKE.<\/p>\n<p>Of course, you could request the field to contain:<\/p>\n<ul>\n<li>&#8216;^Ocean$|^River$|^Lake$&#8217; if you wanted one of these words to be the only word in the field.<\/li>\n<li>^Ocean|^River|^Lake&#8217; if you want the first word of the field to be Ocean, River or Lake.<\/li>\n<li>[a-z ]+ if you simply want to know that there is some view defined.<\/li>\n<\/ul>\n<p>OMG, you can create any of the wonderful expressions that can be built with the patterns from the following table &#8230;<\/p>\n<p>Try your hand at it, knowing you have the following features to build your regular expression:<\/p>\n<table id=\"patterns\">\n<tbody>\n<tr>\n<td>Pattern<\/td>\n<td>What the pattern matches<\/td>\n<\/tr>\n<tr>\n<td>^<\/td>\n<td>Beginning of string<\/td>\n<\/tr>\n<tr>\n<td>$<\/td>\n<td>End of string<\/td>\n<\/tr>\n<tr>\n<td>.<\/td>\n<td>Any single character<\/td>\n<\/tr>\n<tr>\n<td>[&#8230;]<\/td>\n<td>Any character listed between the square brackets<\/td>\n<\/tr>\n<tr>\n<td>[^&#8230;]<\/td>\n<td>Any character not listed between the square brackets<\/td>\n<\/tr>\n<tr>\n<td>[a-z]<\/td>\n<td>Within[], dash (-) create a range of contiguous characters or numbers. Here we are specifying all characters from a to z and A to Z. MySQL ignores character case when using expressions<\/td>\n<\/tr>\n<tr>\n<td>p1|p2|p3<\/td>\n<td>Alternation; matches any of the patterns p1, p2, or p3<\/td>\n<\/tr>\n<tr>\n<td>*<\/td>\n<td>Zero or more instances of preceding element<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>One or more instances of preceding element<\/td>\n<\/tr>\n<tr>\n<td>{n}<\/td>\n<td><em>n<\/em> instances of preceding element<\/td>\n<\/tr>\n<tr>\n<td>{m,n}<\/td>\n<td><em>m<\/em> through <em>n<\/em> instances of preceding element<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In addition to the patterns used in building a filter, you should know that MySQL recognizes the escapes in strings. Therefore you can use \\n,\\r,\\t,\\$,\\^,\\\\, etc to embed special characters in the search and to search for ^,$ and \\.*<\/p>\n<p>To finish off this memo, you need to know:<\/p>\n<ul>\n<li>RLIKE (not to be confused with LIKE) is a synonym for REGEXP in mySQL<\/li>\n<li>MySQL examines expressions independent of the character case.<\/li>\n<li>Oracle&#8217;s REGEXP does not support escape characters found above.<\/li>\n<li>You could install <a href=\"https:\/\/github.com\/mysqludf\/lib_mysqludf_preg\">LIB_MYSQLUDF_PREG<\/a>if you wanted an even more powerful expression handling in your mySQL<\/li>\n<li>You can learn more about LIB_MYSQLUDF_PREG at <a>http:\/\/www.regular-expressions.info\/mysql.html<\/a><\/li>\n<\/ul>\n<\/div>\n<style type=\"text\/css\"><!--\n#patterns { width: 550px ; margin: 20px auto ; collapse-border: collapse ; border: 1px solid black !important ; }     #patterns tr { collapse-border: collapse ; border: 1px solid black !important ; padding: 2px ; }     #patterns td { width: 50% ; collapse-border: collapse ; border: 1px solid black !important ; padding: 4px  20px ; line-height: 16px ; }     #patterns tr:nth-child(even) { background: #ffffff ; }     #patterns tr:nth-child(odd)  { background: #f1f1f1 ;  }     #patterns td em { font-weight: bold ; }     #patterns td p { margin: 0px 20px ; }     #posting h1 { font-size: 20px ; }\n--><\/style>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I needed a MySQL conditionals that I had not used previously. \u00a0 I needed to add a condition that included an OR condition on a single field.\u00a0 While OR is available in the WHERE conditional, it along with the LIKE filter overly complicated my query. \u00a0 Some OR conditionals become very complicated with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-232","post","type-post","status-publish","format-standard","hentry","category-mysql"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.advancedwebhelp.com\/blog\/wp-json\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.advancedwebhelp.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.advancedwebhelp.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.advancedwebhelp.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.advancedwebhelp.com\/blog\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":2,"href":"https:\/\/www.advancedwebhelp.com\/blog\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":344,"href":"https:\/\/www.advancedwebhelp.com\/blog\/wp-json\/wp\/v2\/posts\/232\/revisions\/344"}],"wp:attachment":[{"href":"https:\/\/www.advancedwebhelp.com\/blog\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.advancedwebhelp.com\/blog\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.advancedwebhelp.com\/blog\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}