{"id":203,"date":"2008-04-10T10:08:00","date_gmt":"2008-04-10T13:08:00","guid":{"rendered":"http:\/\/helbertt.16mb.com\/site\/index.php\/2008\/04\/10\/quer-sortear-um-registro-do-banco-de-dados\/"},"modified":"2008-04-10T10:08:00","modified_gmt":"2008-04-10T13:08:00","slug":"quer-sortear-um-registro-do-banco-de-dados","status":"publish","type":"post","link":"https:\/\/helbertt.xyz\/site\/quer-sortear-um-registro-do-banco-de-dados\/","title":{"rendered":"Quer sortear um registro do banco de dados?"},"content":{"rendered":"<p>Ol\u00e1 a todos.<\/p>\n<p>Neste post algo n\u00e3o muito usual, mas \u00e9 no m\u00ednimo muito legal.<\/p>\n<p>Alguma vez voc\u00ea j\u00e1 precisou fazer uma esp\u00e9cie de &#8216;sorteio&#8217; no banco de dados? Tudo bem, eu admito que dificilmente ser\u00e1 o caso.<\/p>\n<p>Mas, se voc\u00ea quiser sortear um felizardo dentro do seu banco de dados voc\u00ea n\u00e3o precisa criar um algoritmo que gera um Random(), testa se existe no banco e caso n\u00e3o exista gere Random() de novo <-- sinceramente isso n\u00e3o d\u00e1! xD\n\nVou colocar abaixo como fazer nos diferentes bancos de dados:\n\n<strong>MySQL<br \/><\/strong><br \/><em><span style=\"color:#3333ff;\">select [colunas] from [tabela] order by rand() limit 1<\/span><\/em><\/p>\n<p><strong>PostgreSQL:<\/strong><\/p>\n<p><span style=\"color:#3333ff;\"><em>select [colunas] from [tabela] order by random() limit 1<\/em><br \/><\/span><br \/><strong>MS SQL Server: <\/strong><\/p>\n<p><em><span style=\"color:#3333ff;\">select top 1 [colunas] from [tabela] order by newid()<\/span><\/em><\/p>\n<p><strong>IBM DB2<\/strong><\/p>\n<p><span style=\"color:#3333ff;\"><em>select [colunas] from [tabela] order by rand() fetch first 1 rows only<\/em><\/span><\/p>\n<p><strong>Oracle:<\/strong><\/p>\n<p><span style=\"color:#3333ff;\"><em>select [colunas] from( select [colunas] from [tabela] order by dbms_random.value ) where rownum = 1<\/em><\/span><\/p>\n<p>Ent\u00e3o valeu. Espero que este post ajude em algo. Abra\u00e7os.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ol\u00e1 a todos. Neste post algo n\u00e3o muito usual, mas \u00e9 no m\u00ednimo muito legal. Alguma vez voc\u00ea j\u00e1 precisou<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout"},"categories":[70,57],"tags":[],"_links":{"self":[{"href":"https:\/\/helbertt.xyz\/site\/wp-json\/wp\/v2\/posts\/203"}],"collection":[{"href":"https:\/\/helbertt.xyz\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/helbertt.xyz\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/helbertt.xyz\/site\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/helbertt.xyz\/site\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":0,"href":"https:\/\/helbertt.xyz\/site\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/helbertt.xyz\/site\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/helbertt.xyz\/site\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/helbertt.xyz\/site\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}