{"id":3342,"date":"2020-07-19T16:59:25","date_gmt":"2020-07-19T19:59:25","guid":{"rendered":"http:\/\/tiagofick-site.test:30000\/?p=3342"},"modified":"2023-01-18T17:59:12","modified_gmt":"2023-01-18T20:59:12","slug":"mysql-group_concat_expr","status":"publish","type":"post","link":"https:\/\/tiagofick.com.br\/site\/mysql-group_concat_expr\/","title":{"rendered":"MySQL &#8211; group_concat(expr)"},"content":{"rendered":"\n<p class=\"has-cyan-bluish-gray-color has-text-color has-small-font-size\"><strong>A alguns dias atr\u00e1s, precisei implementar, utilizando SQL (MySQL), uma solu\u00e7\u00e3o onde era exibido em tela uma conta banc\u00e1ria com seu saldo e seus lan\u00e7amentos futuros. O saldo j\u00e1 era calculado previamente no banco, e os lan\u00e7amentos futuros, resolvi agrup\u00e1-los via SQL em uma string JSON com a fun\u00e7\u00e3o&nbsp;GROUP_CONCAT(<u>expr<\/u>).<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p class=\"has-small-font-size\">J\u00e1 que os registros necess\u00e1rios j\u00e1 eram retornados, n\u00e3o havia necessidade de prejudicar a performance com uma sub-query ou algum join mirabolante. Bastava comparar as datas para saber quais registros exibir como lan\u00e7amento futuro.<\/p>\n\n\n\n<p class=\"has-small-font-size\">Tudo funcionava bem e a coluna&nbsp;<strong>lancamentos_futuros&nbsp;<\/strong>retornava perfeitamente os campos e valores, at\u00e9 que em uma ocasi\u00e3o, houveram muitos registros de lan\u00e7amentos futuros. A string JSON passou a ser interrompida de maneira que &#8220;quebrava&#8221; a estrutura\/sintaxe JSON, me retornando um erro http 500.<\/p>\n\n\n\n<p class=\"has-small-font-size\">Foi ent\u00e3o que a document\u00e7\u00e3o do MySQL me ajudou indicando o caminho:<\/p>\n\n\n\n<p class=\"has-text-color has-background has-small-font-size\" style=\"color:#666666;background-color:#eeeeee\"><em><strong>&#8220;&#8230;The result is truncated to the maximum length that is given by the&nbsp;group_concat_max_len&nbsp;system variable, which has a default value of&nbsp;1024&#8221;.<\/strong><\/em><\/p>\n\n\n\n<p class=\"has-small-font-size\">Para que possa permitir mais de 1024 caracteres nessa fun\u00e7\u00e3o de agrupamento, deve configurar as seguintes vari\u00e1veis do MySQL:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>group_concat_max_len: Tamanho m\u00e1ximo da string em caracteres permitido pela fun\u00e7\u00e3o<\/li>\n\n\n\n<li>max_allowed_packet: tamanho m\u00e1ximo, em bytes, que o buffer pode atingir, \u00e9 recomendado aumentar caso sejam utilizados campos muito longos, ou do tipo BLOB.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-small-font-size\">Bom, no meu caso, foram esses os problemas ocorridos. Espero ter ajudado!&nbsp;At\u00e9 o pr\u00f3ximo post!<\/p>\n\n\n\n<p class=\"has-normal-font-size\"><strong>Refer\u00eancias<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/aggregate-functions.html#function_group-concat\" target=\"_blank\">Aggregate Functions &#8211; Group Concat &#8211; Mysql<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/server-system-variables.html#sysvar_max_allowed_packet\" target=\"_blank\">Set Variables &#8211; max_allowed_packet<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/server-system-variables.html#sysvar_group_concat_max_len\" target=\"_blank\">Set Variables &#8211; group_concat_max_len<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A alguns dias atr\u00e1s, precisei implementar, utilizando SQL (MySQL), uma solu\u00e7\u00e3o onde era exibido em tela uma conta banc\u00e1ria com seu saldo e seus lan\u00e7amentos futuros. O saldo j\u00e1 era&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":3420,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3342","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-banco-de-dados"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/tiagofick.com.br\/site\/wp-json\/wp\/v2\/posts\/3342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tiagofick.com.br\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tiagofick.com.br\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tiagofick.com.br\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tiagofick.com.br\/site\/wp-json\/wp\/v2\/comments?post=3342"}],"version-history":[{"count":1,"href":"https:\/\/tiagofick.com.br\/site\/wp-json\/wp\/v2\/posts\/3342\/revisions"}],"predecessor-version":[{"id":3412,"href":"https:\/\/tiagofick.com.br\/site\/wp-json\/wp\/v2\/posts\/3342\/revisions\/3412"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tiagofick.com.br\/site\/wp-json\/wp\/v2\/media\/3420"}],"wp:attachment":[{"href":"https:\/\/tiagofick.com.br\/site\/wp-json\/wp\/v2\/media?parent=3342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tiagofick.com.br\/site\/wp-json\/wp\/v2\/categories?post=3342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tiagofick.com.br\/site\/wp-json\/wp\/v2\/tags?post=3342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}