MySQL select random records with limitShould I use the datetime or timestamp data type in MySQL?Select variable number of random records from MySQLFind duplicate records in MySQLHow to get a list of user accounts using the command line in MySQL?Retrieving the last record in each group - MySQLWhy does MYSQL higher LIMIT offset slow the query down?Reference - What does this error mean in PHP?How do I import an SQL file using the command line in MySQL?Show unique random records within LIMITmySQL - Select records based on value in order, then random records in one query

How do I solve such questions on paramagnetism and ferromagnetism?

When did J.K. Rowling decide to make Ron and Hermione a couple?

Why are sugars in whole fruits not digested the same way sugars in juice are?

Does KNN have a loss function?

How to draw twisted cuves?

Applying for mortgage when living together but only one will be on the mortgage

Feedback diagram

Transistor design with beta variation

Can an alphabet for a Turing machine contain subsets of other alphabets?

How to structure presentation to avoid getting questions that will be answered later in the presentation?

What's the proper way of indicating that a car has reached its destination during a dialogue?

Can living where (rare) earth magnetic ore is abundant provide any protection from cosmic radiation?

Can I shorten this filter, that finds disk sizes over 100G?

What does the "きゃ" in "していきゃがらなかった" stand for?

HackerRank Implement Queue using two stacks Solution

Overprovisioning SSD on ubuntu. How? Ubuntu 19.04 Samsung SSD 860

Why is “deal 6 damage” a legit phrase?

Move label of an angle in Tikz

Why interlaced CRT scanning wasn't done back and forth?

Went to a big 4 but got fired for underperformance in a year recently - Now every one thinks I'm pro - How to balance expectations?

What is the reason behind water not falling from a bucket at the top of loop?

Backpacking with incontinence

Is Illustrator accurate for business card sizes?

What do the screens say after you are set free?



MySQL select random records with limit


Should I use the datetime or timestamp data type in MySQL?Select variable number of random records from MySQLFind duplicate records in MySQLHow to get a list of user accounts using the command line in MySQL?Retrieving the last record in each group - MySQLWhy does MYSQL higher LIMIT offset slow the query down?Reference - What does this error mean in PHP?How do I import an SQL file using the command line in MySQL?Show unique random records within LIMITmySQL - Select records based on value in order, then random records in one query






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a query for selecting random records with a limit of 6.



$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT " . $limit);


If I set the limit to 6, it will sometimes only show 4 records, sometimes 5.

How can I make it always show 6?

I have more than 6 records in the database.



I looked at some questions around this here but couldn't find a clear answer.










share|improve this question





















  • 2





    Mmm... I am not able to reproduce your issue.

    – GMB
    Mar 27 at 0:32






  • 2





    @GMB perfect reason to vote to close as non-reproducible...

    – Nick
    Mar 27 at 0:40











  • Perhaps the problem is in your application layer? Can you share more of the PHP code?

    – Nick
    Mar 27 at 0:42






  • 1





    ORDER BY RAND() is also a fantastic way to exponentially tank your DB performance as your data set grows.

    – Sammitch
    Mar 27 at 0:50






  • 1





    @devManMan understand. But there is no problem with the code you have posted, so for anyone to help you with the problem you will need to post more of your PHP code.

    – Nick
    Mar 27 at 1:12

















0















I have a query for selecting random records with a limit of 6.



$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT " . $limit);


If I set the limit to 6, it will sometimes only show 4 records, sometimes 5.

How can I make it always show 6?

I have more than 6 records in the database.



I looked at some questions around this here but couldn't find a clear answer.










share|improve this question





















  • 2





    Mmm... I am not able to reproduce your issue.

    – GMB
    Mar 27 at 0:32






  • 2





    @GMB perfect reason to vote to close as non-reproducible...

    – Nick
    Mar 27 at 0:40











  • Perhaps the problem is in your application layer? Can you share more of the PHP code?

    – Nick
    Mar 27 at 0:42






  • 1





    ORDER BY RAND() is also a fantastic way to exponentially tank your DB performance as your data set grows.

    – Sammitch
    Mar 27 at 0:50






  • 1





    @devManMan understand. But there is no problem with the code you have posted, so for anyone to help you with the problem you will need to post more of your PHP code.

    – Nick
    Mar 27 at 1:12













0












0








0








I have a query for selecting random records with a limit of 6.



$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT " . $limit);


If I set the limit to 6, it will sometimes only show 4 records, sometimes 5.

How can I make it always show 6?

I have more than 6 records in the database.



I looked at some questions around this here but couldn't find a clear answer.










share|improve this question
















I have a query for selecting random records with a limit of 6.



$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT " . $limit);


If I set the limit to 6, it will sometimes only show 4 records, sometimes 5.

How can I make it always show 6?

I have more than 6 records in the database.



I looked at some questions around this here but couldn't find a clear answer.







php mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 0:31









b.enoit.be

5,5944 gold badges28 silver badges42 bronze badges




5,5944 gold badges28 silver badges42 bronze badges










asked Mar 27 at 0:24









devManMandevManMan

256 bronze badges




256 bronze badges










  • 2





    Mmm... I am not able to reproduce your issue.

    – GMB
    Mar 27 at 0:32






  • 2





    @GMB perfect reason to vote to close as non-reproducible...

    – Nick
    Mar 27 at 0:40











  • Perhaps the problem is in your application layer? Can you share more of the PHP code?

    – Nick
    Mar 27 at 0:42






  • 1





    ORDER BY RAND() is also a fantastic way to exponentially tank your DB performance as your data set grows.

    – Sammitch
    Mar 27 at 0:50






  • 1





    @devManMan understand. But there is no problem with the code you have posted, so for anyone to help you with the problem you will need to post more of your PHP code.

    – Nick
    Mar 27 at 1:12












  • 2





    Mmm... I am not able to reproduce your issue.

    – GMB
    Mar 27 at 0:32






  • 2





    @GMB perfect reason to vote to close as non-reproducible...

    – Nick
    Mar 27 at 0:40











  • Perhaps the problem is in your application layer? Can you share more of the PHP code?

    – Nick
    Mar 27 at 0:42






  • 1





    ORDER BY RAND() is also a fantastic way to exponentially tank your DB performance as your data set grows.

    – Sammitch
    Mar 27 at 0:50






  • 1





    @devManMan understand. But there is no problem with the code you have posted, so for anyone to help you with the problem you will need to post more of your PHP code.

    – Nick
    Mar 27 at 1:12







2




2





Mmm... I am not able to reproduce your issue.

– GMB
Mar 27 at 0:32





Mmm... I am not able to reproduce your issue.

– GMB
Mar 27 at 0:32




2




2





@GMB perfect reason to vote to close as non-reproducible...

– Nick
Mar 27 at 0:40





@GMB perfect reason to vote to close as non-reproducible...

– Nick
Mar 27 at 0:40













Perhaps the problem is in your application layer? Can you share more of the PHP code?

– Nick
Mar 27 at 0:42





Perhaps the problem is in your application layer? Can you share more of the PHP code?

– Nick
Mar 27 at 0:42




1




1





ORDER BY RAND() is also a fantastic way to exponentially tank your DB performance as your data set grows.

– Sammitch
Mar 27 at 0:50





ORDER BY RAND() is also a fantastic way to exponentially tank your DB performance as your data set grows.

– Sammitch
Mar 27 at 0:50




1




1





@devManMan understand. But there is no problem with the code you have posted, so for anyone to help you with the problem you will need to post more of your PHP code.

– Nick
Mar 27 at 1:12





@devManMan understand. But there is no problem with the code you have posted, so for anyone to help you with the problem you will need to post more of your PHP code.

– Nick
Mar 27 at 1:12












2 Answers
2






active

oldest

votes


















0














$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT $limit " );





share|improve this answer

























  • Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

    – CertainPerformance
    Mar 27 at 3:47


















-2














Try with :
SELECT RAND(6)



$query = $this->pdo->prepare("SELECT * FROM " . $this->table . " ORDER BY RAND(6) LIMIT " . $limit);






share|improve this answer




















  • 1





    The parameter to RAND merely changes the seed from which the random numbers are generaed and would have no effect on the result of the query.

    – Nick
    Mar 27 at 0:43













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368059%2fmysql-select-random-records-with-limit%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT $limit " );





share|improve this answer

























  • Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

    – CertainPerformance
    Mar 27 at 3:47















0














$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT $limit " );





share|improve this answer

























  • Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

    – CertainPerformance
    Mar 27 at 3:47













0












0








0







$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT $limit " );





share|improve this answer













$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT $limit " );






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 1:38









getphpgetphp

35 bronze badges




35 bronze badges















  • Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

    – CertainPerformance
    Mar 27 at 3:47

















  • Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

    – CertainPerformance
    Mar 27 at 3:47
















Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

– CertainPerformance
Mar 27 at 3:47





Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

– CertainPerformance
Mar 27 at 3:47













-2














Try with :
SELECT RAND(6)



$query = $this->pdo->prepare("SELECT * FROM " . $this->table . " ORDER BY RAND(6) LIMIT " . $limit);






share|improve this answer




















  • 1





    The parameter to RAND merely changes the seed from which the random numbers are generaed and would have no effect on the result of the query.

    – Nick
    Mar 27 at 0:43















-2














Try with :
SELECT RAND(6)



$query = $this->pdo->prepare("SELECT * FROM " . $this->table . " ORDER BY RAND(6) LIMIT " . $limit);






share|improve this answer




















  • 1





    The parameter to RAND merely changes the seed from which the random numbers are generaed and would have no effect on the result of the query.

    – Nick
    Mar 27 at 0:43













-2












-2








-2







Try with :
SELECT RAND(6)



$query = $this->pdo->prepare("SELECT * FROM " . $this->table . " ORDER BY RAND(6) LIMIT " . $limit);






share|improve this answer













Try with :
SELECT RAND(6)



$query = $this->pdo->prepare("SELECT * FROM " . $this->table . " ORDER BY RAND(6) LIMIT " . $limit);







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 0:37









Med LaggMed Lagg

272 bronze badges




272 bronze badges










  • 1





    The parameter to RAND merely changes the seed from which the random numbers are generaed and would have no effect on the result of the query.

    – Nick
    Mar 27 at 0:43












  • 1





    The parameter to RAND merely changes the seed from which the random numbers are generaed and would have no effect on the result of the query.

    – Nick
    Mar 27 at 0:43







1




1





The parameter to RAND merely changes the seed from which the random numbers are generaed and would have no effect on the result of the query.

– Nick
Mar 27 at 0:43





The parameter to RAND merely changes the seed from which the random numbers are generaed and would have no effect on the result of the query.

– Nick
Mar 27 at 0:43

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368059%2fmysql-select-random-records-with-limit%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?