{"id":38,"date":"2012-03-02T15:44:24","date_gmt":"2012-03-02T15:44:24","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=38"},"modified":"2012-03-04T13:21:42","modified_gmt":"2012-03-04T13:21:42","slug":"sql-select-a-random-row-record","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/sql-select-a-random-row-record\/","title":{"rendered":"SQL &#8211; Select a random row \/ record"},"content":{"rendered":"<p>I&#39;ve seen many posts on this topic, but none of the seem to account for the fact that your primary key may not start at the number 1.<\/p>\n<p>In your finds, you may have come across the following method, which works and is efficient, if your primary key begins with 1.<\/p>\n<p><code>SELECT question_id, question_name<br \/>\n\tFROM question<br \/>\n\tWHERE question_id &gt;= (SELECT FLOOR(MAX(question_id)&nbsp; * RAND()) FROM question)<br \/>\n\tLIMIT 1<br \/>\n\t<\/code><\/p>\n<p>The method below will &quot;detect&quot; a primary key which may start at any integer.<\/p>\n<p><code>SELECT question_id, question_name<br \/>\n\tFROM question<br \/>\n\tWHERE question_id &gt;= (SELECT FLOOR((MAX(question_id) - MIN(question_id) + 1) * RAND()) + MIN(question_id) FROM question)<br \/>\n\tORDER BY question_id<br \/>\n\tLIMIT 1<br \/>\n\t<\/code><\/p>\n<p>This may seem obvious, but as a further optimization, consider using a constant in place of <code>MAX(id)<\/code> This method only makes sense if you are using a table with a fixed number of rows (records are never or rarely changed.) Using the original query as an example:<\/p>\n<p><code>SELECT question_id, question_name<br \/>\n\tFROM question<br \/>\n\tWHERE question_id &gt;= (SELECT FLOOR(30000&nbsp; * RAND()) FROM question)<br \/>\n\tLIMIT 1<\/code><\/p>\n<p>Or, if you find that your scripting languages random number function is faster than your databases&#39;s, simply generate the random number and multiply it by your row number constant directly in your script. Then, pass that number to your query.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#39;ve seen many posts on this topic, but none of the seem to account for the fact that your primary key may not start at&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[13],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/38"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":5,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":525,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions\/525"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}