This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Selecting Random Rows in MySQL
I'm creating a simple web application using PHP and MySQL. In it, I need to randomly select a small set of rows from a table in a random order. How can I achieve such thing using MySQL?
SELECT * FROM table ORDER BY RAND() LIMIT 10;
Edit:
Useful information about the MySQL RAND() function can be found here.
select * from table order by rand() limit 10
Note that order by rand() with large dataset is very slow but in your case it's not a problem.
you could do that using RAND() function .
SELECT questine FROM tablename ORDER BY RAND() LIMIT 10
will select 10 questines at random under assumption the questine is stored under field questine
Related
This question already has answers here:
Select last row in MySQL
(11 answers)
Closed last year.
id
first_die
24
2
25
6
How do i return the most recent first_die in this set? I want to display the latest roll in my select query. I know returning the latest first_die will be based on id but I'm having difficulty doing this because it is a simple fix, but I'm looking at not so simple answers to the problem. Any help would be nice. Thank you.
Use:
select id,first_die
from test_tbl
order by id desc limit 1 ;
Demo:
Or:
select id,first_die
from test_tbl
where id = (select max(id) from test_tbl);
Demo:
If your most recent id is the maximum number then you would use SELECT MAX(id) query (sql query).
If you want to return the entire recordset or row use the code below
SELECT * FROM tablename WHERE id=(SELECT MAX(id) FROM tablename);
This question already has answers here:
Select last N rows from MySQL
(6 answers)
Closed 6 years ago.
SELECT FROM users.id,username ORDER BY ID ASC LIMIT 3
Not working in output,
this line not error showing
how to correct line ?
it should be
SELECT id, username FROM users ORDER BY ID ASC LIMIT 3
You would be better served writing the following...
SELECT id FROM users ORDER BY id ASC LIMIT 3
Your original query is wrong, SQL, like any programming language, needs to be written EXACTLY as intended.
Please do an introductory course on SQL.
This question already has answers here:
MySQL select 10 random rows from 600K rows fast
(28 answers)
Closed 9 years ago.
Is it true that ORDER BY rand() performance is very slow compared to other solutions? If yes, what are better ways to select random row(s) from the database?
My query:
SELECT sName FROM bpoint WHERE placeID=? ORDER BY rand() LIMIT 1;
Yes, ORDER BY RAND() can be very slow in larger result-sets.
An option is to fetch resultset with this statement (into an array):
SELECT sName FROM bpoint WHERE placeID=?;
After that - use array_rand($resultset) to get a randomized item from the $resultset query.
This question already has answers here:
How to request a random row in SQL?
(30 answers)
Closed 8 years ago.
I have names in my database and I want to draw a name for a contest.
Anyone have an idea for that ?
Thanks !!
SELECT * FROM table WHERE num_value >= RAND() * (SELECT MAX(num_value) FROM table) LIMIT 1
This works in constant time, regardless of the table size, if num_value is indexed. One caveat: this assumes that num_value is equally distributed in the range 0..MAX(num_value). If your dataset strongly deviates from this assumption, you will get skewed results (some rows will appear more often than others).
A query like this could work
SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
MySQL select 10 random rows from 600K rows fast
I need to pull from a table a random id. At this moment only a few records exists but with time they will grow. What methods of getting this id are in Php or MySql, and what are the trade offs, consequences between them. One last thing i need speed and performance.
select * from YOUR_TABLE order by rand() limit 1
You can achieve this direct in your SQL:
SELECT `idfield` FROM `table` ORDER BY RAND() LIMIT 0,1;
Also see here for some alternatives.
This will be a simple means of execution than building the randomisation in your PHP and then passing to mySQL, though the link above details the merits of the various approaches.
SELECT id FROM table ORDER BY RAND() LIMIT 1
What this does is basically: take the table, order the records by a random number, and get the first record's ID.
There is already a lot of questions and answer about that:
Random Records Mysql PHP
mysql query with random and desc
If you have less that 500,000 records, the RAND() is perfectly fine.
SELECT * FROM tbl_name ORDER BY RAND() LIMIT 1
Well you can obtain a random number with RAND(), but you would still have to call that method/function until you actually get something (you can have 1,2,5,6,100 as id`s due to deletion).
there is a post on this http://jan.kneschke.de/projects/mysql/order-by-rand/
Procedures can be usefull (but said to be slow...):
1 var to get the maximum value
then produce a random within 1 and that interval (or get the minimum)
then query