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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현