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;
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
|
show 4 more comments
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
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
|
show 4 more comments
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
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
php mysql
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
|
show 4 more comments
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
|
show 4 more comments
2 Answers
2
active
oldest
votes
$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT $limit " );
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
add a comment |
Try with :
SELECT RAND(6)
$query = $this->pdo->prepare("SELECT * FROM " . $this->table . "
ORDER BY RAND(6) LIMIT " . $limit);
1
The parameter toRAND
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT $limit " );
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
add a comment |
$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT $limit " );
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
add a comment |
$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT $limit " );
$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT $limit " );
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
add a comment |
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
add a comment |
Try with :
SELECT RAND(6)
$query = $this->pdo->prepare("SELECT * FROM " . $this->table . "
ORDER BY RAND(6) LIMIT " . $limit);
1
The parameter toRAND
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
add a comment |
Try with :
SELECT RAND(6)
$query = $this->pdo->prepare("SELECT * FROM " . $this->table . "
ORDER BY RAND(6) LIMIT " . $limit);
1
The parameter toRAND
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
add a comment |
Try with :
SELECT RAND(6)
$query = $this->pdo->prepare("SELECT * FROM " . $this->table . "
ORDER BY RAND(6) LIMIT " . $limit);
Try with :
SELECT RAND(6)
$query = $this->pdo->prepare("SELECT * FROM " . $this->table . "
ORDER BY RAND(6) LIMIT " . $limit);
answered Mar 27 at 0:37
Med LaggMed Lagg
272 bronze badges
272 bronze badges
1
The parameter toRAND
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
add a comment |
1
The parameter toRAND
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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