{"id":68,"date":"2010-01-12T23:06:42","date_gmt":"2010-01-13T04:06:42","guid":{"rendered":"http:\/\/signaturewebmedia.com\/blog\/?p=68"},"modified":"2010-01-13T03:59:57","modified_gmt":"2010-01-13T08:59:57","slug":"mysql-case-insesitive-group-by","status":"publish","type":"post","link":"http:\/\/signaturewebmedia.com\/blog\/68\/mysql-case-insesitive-group-by\/","title":{"rendered":"MySQL: case insesitive &#8220;GROUP BY&#8221;"},"content":{"rendered":"<p><a href=\"http:\/\/getfreeipad.us\/info\/offer1\/\" target=\"_blank\" rel=\"nofollow\"><img src=\"http:\/\/getfreeipad.us\/images\/ipad-tester-wanted.jpg\" border=\"0\"><\/a><\/p><p>When we were building some SQL statements to gather some statistics and realized that the &#8220;GROUP BY&#8221; clause is NOT case sensitive on MySQL&#8230; (we are using utf8_unicode_ci as a character set collation)<\/p>\n<p>i.e.<\/p>\n<blockquote><p>mysql&gt; select keyword, count(*) from TopKeywords<br \/>\n-&gt; where lower(keyword) like &#8216;secured loans&#8217;<br \/>\n-&gt; group by keyword;<\/p>\n<p>+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+<br \/>\n| keyword\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | count(*) |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+<br \/>\n| secured loans |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4 |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+<br \/>\n1 row in set (0.50 sec)<\/p>\n<p>I know the max count(*) result should be 3 if the key is case sensitive.<\/p><\/blockquote>\n<p>We found that this note: <a title=\"MySQL Bugs: #24838: group by compares strings case-insensitively\" rel=\"nofollow\" href=\"http:\/\/bugs.mysql.com\/bug.php?id=24838\" target=\"_blank\">http:\/\/bugs.mysql.com\/bug.php?id=24838<\/a><\/p>\n<p>According to the note above, it is NOT a bug; however, it does not seem like it is giving a workaround.<\/p>\n<p>Scratching our head a little and found a easy solution if you need to use the case sensitive GROUP BY clause. The thing is that you need to group by absolutely unique value. In our case,\u00a0 used hex() function:<\/p>\n<blockquote><p>mysql&gt; select keyword, hex(keyword), count(*) from TopKeywords<br \/>\n-&gt; where lower(keyword) like &#8216;secured loans&#8217;<br \/>\n-&gt; group by hex(keyword);<\/p>\n<p>+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+<br \/>\n| keyword\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | hex(keyword)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | count(*) |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+<br \/>\n| Secured Loans | 53656375726564204C6F616E73 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1 |<br \/>\n| secured loans | 73656375726564206C6F616E73 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3 |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+<br \/>\n2 rows in set (0.48 sec)<\/p><\/blockquote>\n<p>We are sure that you can use some other functions to achieve case sensitive group by effects.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we were building some SQL statements to gather some statistics and realized that the &#8220;GROUP BY&#8221; clause is NOT case sensitive on MySQL&#8230; (we are using utf8_unicode_ci as a character set collation)<br \/>\ni.e.<br \/>\nmysql&gt; select keyword, count(*) from TopKeywords<br \/>\n-&gt; where lower(keyword) like &#8216;secured loans&#8217;<br \/>\n-&gt; group by keyword;<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+<br \/>\n| keyword\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | count(*) |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+<br \/>\n| secured loans |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4 |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+<br \/>\n1 row in set (0.50 sec)<br \/>\nI know the max count(*) result should be 3 if the key is case sensitive.<br \/>\nWe found that this note: http:\/\/bugs.mysql.com\/bug.php?id=24838<\/p>\n<p>According to the note above, it is NOT a bug; however, it does not seem like it is giving a workaround.<br \/>\nScratching our head a little and found a easy solution if you need to use the case sensitive GROUP BY clause. The thing is that you need to group by absolutely unique value. In our case,\u00a0 used hex() function:<br \/>\nmysql&gt; select keyword, hex(keyword), count(*) from TopKeywords<br \/>\n-&gt; where lower(keyword) like &#8216;secured loans&#8217;<br \/>\n-&gt; group by hex(keyword);<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+<br \/>\n| keyword\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | hex(keyword)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | count(*) |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+<br \/>\n| Secured Loans | 53656375726564204C6F616E73 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1 |<br \/>\n| secured loans | 73656375726564206C6F616E73 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3 |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+<br \/>\n2 rows in set (0.48 sec)<br \/>\nWe are sure that you can use some other functions to achieve case sensitive group by effects.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/getfreeipad.us\/info\/offer2\/\" target=\"_blank\" rel=\"nofollow\">\r\n<IMG SRC=\"http:\/\/www.mb01.com\/getimage.asp?m=1753&o=2883&i=39158.dat\" width=300 height=250 border=0>\r\n<\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[20,21],"tags":[24,23,196,26,27,25],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/posts\/68"}],"collection":[{"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":4,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":70,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions\/70"}],"wp:attachment":[{"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}