SkyRanger Posted September 17, 2019 Share Posted September 17, 2019 Right now I have a little over 400 entries but I am trying to figure out how to only pull 1 entry from the last 10 inserted. Currently I have: SELECT * FROM $tablename ORDER BY RAND() LIMIT 1 I do know I need to do order by kuid asc but not sure how to pull 1 out of the last 10 inserted randomly on page refresh. Quote Link to comment https://forums.phpfreaks.com/topic/309243-select-random-but-limit-1-to-show-from-last-10-entries/ Share on other sites More sharing options...
Barand Posted September 17, 2019 Share Posted September 17, 2019 (edited) SELECT * FROM ( SELECT * FROM $tablename ORDER BY id DESC LIMIT 10 ) last10 ORDER BY RAND() LIMIT 1; (if recs have timestamp, use that to get latest 10 records) Edited September 17, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309243-select-random-but-limit-1-to-show-from-last-10-entries/#findComment-1569785 Share on other sites More sharing options...
SkyRanger Posted September 18, 2019 Author Share Posted September 18, 2019 It does Barand. Thank you. Shows how much I know about sql. Did not know you could use a select within a select. Learning something new every day. Quote Link to comment https://forums.phpfreaks.com/topic/309243-select-random-but-limit-1-to-show-from-last-10-entries/#findComment-1569795 Share on other sites More sharing options...
benanamen Posted September 18, 2019 Share Posted September 18, 2019 3 hours ago, SkyRanger said: select within a select It is called a Subquery. Now you know the proper term so you can look it up and learn about it. Quote Link to comment https://forums.phpfreaks.com/topic/309243-select-random-but-limit-1-to-show-from-last-10-entries/#findComment-1569798 Share on other sites More sharing options...
Barand Posted September 18, 2019 Share Posted September 18, 2019 5 hours ago, SkyRanger said: Did not know you could use a select within a select. Learning something new every day That particular subquery creates a temporary table called "last10" which contains your last ten records. See the link in my sig. Quote Link to comment https://forums.phpfreaks.com/topic/309243-select-random-but-limit-1-to-show-from-last-10-entries/#findComment-1569802 Share on other sites More sharing options...
SkyRanger Posted September 18, 2019 Author Share Posted September 18, 2019 Thank you to the both of you. I am almost 50, doing this late in life and self teaching with the help of great people like you. Thank you again. Quote Link to comment https://forums.phpfreaks.com/topic/309243-select-random-but-limit-1-to-show-from-last-10-entries/#findComment-1569813 Share on other sites More sharing options...
Barand Posted September 18, 2019 Share Posted September 18, 2019 2 minutes ago, SkyRanger said: I am almost 50 Ah! Those were the days. Oh to be young again 😊 Quote Link to comment https://forums.phpfreaks.com/topic/309243-select-random-but-limit-1-to-show-from-last-10-entries/#findComment-1569814 Share on other sites More sharing options...
SkyRanger Posted September 18, 2019 Author Share Posted September 18, 2019 34 minutes ago, Barand said: Ah! Those were the days. Oh to be young again 😊 Yeah I forgot old feller...lol Quote Link to comment https://forums.phpfreaks.com/topic/309243-select-random-but-limit-1-to-show-from-last-10-entries/#findComment-1569815 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.