{"id":2046,"date":"2014-12-09T14:59:46","date_gmt":"2014-12-09T22:59:46","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2046"},"modified":"2015-04-07T08:46:02","modified_gmt":"2015-04-07T15:46:02","slug":"miscellaneous-mysql","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2046","title":{"rendered":"Miscellaneous MySQL"},"content":{"rendered":"<h2>Case sensitive selection<\/h2>\n<p>These three methods are equivalent.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT name FROM `customers` WHERE CAST(name AS BINARY) LIKE &#039;%Net%&#039;\n\nSELECT name FROM `customers` WHERE name COLLATE utf8_bin LIKE &#039;%Net%&#039;\n\nSELECT name FROM `customers` WHERE name LIKE BINARY &#039;%Net%&#039;\n<\/code><\/pre><\/p>\n<p>It also works for non-latin characters. This search will find only names with an \u201c\u00f1\u201d in the name. If you leave out the BINARY you will get all the names with an \u201cn\u201d in the name.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT name FROM `customers` WHERE name LIKE BINARY &#039;%Mu\u00f1oz%&#039;\n<\/code><\/pre><\/p>\n<h2>BETWEEN<\/h2>\n<p>You can search fields containing numbers or characters using BETWEEN.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT * FROM `customers` WHERE id BETWEEN 900 AND 999\n<\/code><\/pre><\/p>\n<p>You can also search a character string of numbers with BETWEEN.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT * FROM `customers` WHERE zip BETWEEN 90000 AND 99999\nSELECT * FROM `customers` WHERE zip*2 BETWEEN 90000 AND 99999\n<\/code><\/pre><br \/>\nThese also work:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT * FROM `customers` WHERE name BETWEEN &#039;Ja&#039; AND &#039;Jz&#039;\nSELECT * FROM `customers` WHERE name BETWEEN &#039;Jam&#039; AND &#039;Jaz&#039;\nSELECT * FROM `customers` WHERE city BETWEEN &#039;A&#039; AND &#039;M&#039;\n<\/code><\/pre><br \/>\nYou might expect this to work, but it doesn\u2019t. The first set of characters must be alphabetically before the second set of characters.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT * FROM `customers` WHERE name BETWEEN &#039;Ja&#039; AND &#039;Hz&#039;\n<\/code><\/pre><\/p>\n<h2>IN<\/h2>\n<p>This could come in handy if you want to restrict searches to just a few of the possible values in a field.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT * FROM `customers` WHERE city IN (&#039;Tulsa&#039;, &#039;Orlando&#039;)\n<\/code><\/pre><\/p>\n<h2>Derived Columns: CONCAT<\/h2>\n<p>mySQL uses the CONCAT function not the concat operator, ||, for concatenation.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT CONCAT(city, &#039;, &#039;, state) AS City_State FROM `customers`\n<\/code><\/pre><br \/>\nThis displays the column heading for the result as City_State, but it is not available for use in a WHERE clause. This fails with an unknown column error:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT CONCAT(city, &#039;, &#039;, state) AS City_State \nFROM `customers` \nWHERE City_State = &#039;Anchorage, AK&#039;\n<\/code><\/pre><br \/>\nHowever, you can wrap the query as follows,<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT *\nFROM (SELECT CONCAT(city, &#039;, &#039;, state) AS City_State \nFROM `customers`) \nAS firstQuery\nWHERE City_State = &#039;Anchorage, AK&#039;\n<\/code><\/pre><\/p>\n<p>This doesn\u2019t work in phpMyAdmin or mySQL but works in other DBMSs:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT * FROM `customers`\nWHERE city || &#039;, &#039; || state = &#039;Anchorage&#039; || &#039;, &#039; || &#039;AK&#039;\n<\/code><\/pre><\/p>\n<h2>Substring gotcha<\/h2>\n<p>If you want to find the cities that start with &#8216;Los&#8217;, you might be tempted to write the SUBSTRING as SUBSTRING(city, 0, 2) like any normal string access e.g. PHP. But MySQL starts with 1 and then has the number of characters you want to extract. So you\u2019d do it like this:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT city FROM `customers` WHERE SUBSTRING(city FROM 1 FOR 3) LIKE &#039;Los&#039;\n<\/code><\/pre><\/p>\n<h2>UPPER and LOWER<\/h2>\n<p>You can transform fields or literals in SELECT, WHERE, and ORDER BY clauses.<\/p>\n<p>Since all of the state abbreviations are in upper case, this works:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT state FROM `customers` WHERE state COLLATE utf8_bin LIKE UPPER(&#039;%ca%&#039;)\n<\/code><\/pre><br \/>\nas does this:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT state FROM `customers` WHERE LOWER(state) COLLATE utf8_bin LIKE&#039;%ca%&#039;\n<\/code><\/pre><\/p>\n<h2>Combining operators<\/h2>\n<p>You might want to uppercase the address for mailing labels, this is one way to do it.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT UPPER(CONCAT(city, &#039;, &#039;, state, &#039; &#039;, zip)) AS City_State_Zip FROM `customers`\n<\/code><\/pre><br \/>\nThe result is<br \/>\n<pre><code class=\"preserve-code-formatting\">\nST. ALBERT, AB T8N 2N9\nANCHORAGE, AK 99503\n<\/code><\/pre><\/p>\n<h2>Interesting Query<\/h2>\n<p>Try this query:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT CURRENT_DATE AS&nbsp;&nbsp;&#039;Date&#039;, CURRENT_TIME AS&nbsp;&nbsp;&#039;Time&#039;,&nbsp;&nbsp;&#039;Well Golly&#039; AS &#039;Site&#039;\n<\/code><\/pre><br \/>\nIt yields one row:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Site\n2014-12-05&nbsp;&nbsp;13:19:38&nbsp;&nbsp;Well Golly\n<\/code><\/pre><br \/>\nThen try this one:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT CURRENT_DATE AS &#039;Date&#039;, CURRENT_TIME AS &#039;Time&#039;, &#039;Well Golly&#039; AS &#039;Site&#039;, name FROM `customers`\n<\/code><\/pre><br \/>\nIt yields one row for each row in the database:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Site&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name\n2014-12-05&nbsp;&nbsp;13:31:59&nbsp;&nbsp;Well Golly Carmen Edsel\n2014-12-05&nbsp;&nbsp;13:31:59&nbsp;&nbsp;Well Golly Jitka Dragonola\n2014-12-05&nbsp;&nbsp;13:31:59&nbsp;&nbsp;Well Golly Deryk Bealer\n<\/code><\/pre><\/p>\n<h2>GROUP BY and COUNT(*)<\/h2>\n<p>When testing this one out, I found some values that shouldn\u2019t be in the state field.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT state, COUNT(*) FROM `customers` GROUP BY state \n\nstate&nbsp;&nbsp; COUNT(*)\nD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1\nda&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1\n<\/code><\/pre><br \/>\nIf you only want selected states, you can use the IN selection criterion:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT state, COUNT(*) \nFROM `customers` \nWHERE state IN (&#039;CA&#039;, &#039;IN&#039;) \nGROUP BY state \n<\/code><\/pre><br \/>\nOr if you want to find the number of customers in the top 20 cities, ordered from most to least.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT city, state, COUNT(*) as count \nFROM `customers` \nGROUP BY city, state \nORDER BY count DESC \nLIMIT 20\n<\/code><\/pre><br \/>\nNote that the SELECT and GROUP BY must have the same fields or you will get incorrect results. In this case, leaving out the state in the group by yields the most customers in Brooklyn, FL <\/p>\n<h2>HAVING<\/h2>\n<p>Use the HAVING search condition to filter the results.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT city, state, COUNT(*) as count \nFROM `customers` \nGROUP BY city, state \nHAVING COUNT(*) &gt; 50 \nORDER BY count DESC\n<\/code><\/pre><br \/>\nHAVING is applied after GROUP BY. Its main use is to filter on aggregates\u2014SUM, COUNT, AVG, etc.<\/p>\n<p>In general, you <b>can\u2019t<\/b> reference aliases in WHERE or GROUP BY clauses, but MySQL allows referencing SELECT level aliases in GROUP BY, ORDER BY and HAVING.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Case sensitive selection These three methods are equivalent. SELECT name FROM `customers` WHERE CAST(name AS BINARY) LIKE &#039;%Net%&#039; SELECT name FROM `customers` WHERE name COLLATE utf8_bin LIKE &#039;%Net%&#039; SELECT name FROM `customers` WHERE name LIKE BINARY &#039;%Net%&#039; It also works for non-latin characters. This search will find only names with an \u201c\u00f1\u201d in the name. &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2046\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Miscellaneous MySQL<\/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-2046","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2046","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=2046"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2046\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}