I am writting plugin where i have custom table. I am trying to access one random row from last 5 rows from this custom table.

"SELECT * FROM $table_name WHERE id > MAX(id)-5 AND id < MAX(id)+1 ORDER BY `time` DESC LIMIT 1"; 

but does not work. Any solution ?

UPDATE problem sorted as;

$ofset = rand(0, 4);
 $read_link_data = "SELECT * FROM $table_name ORDER BY `time` DESC LIMIT $ofset,1";

it works but i suspect is not optimal


"SELECT column FROM table ORDER BY RAND() LIMIT 1 " or you can add more restrictions to fetch from last 5 by time. Hope it help you a bit

  • thanks. i had mistake. now is corrected but still does not work. i need to find the max id number and then create range from which single row will be selected. any clue what is wrong? – Greg Skala Apr 8 '17 at 9:42
  • What you are getting when you run sql – inrsaurabh Apr 8 '17 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.