How to echo random row from database? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL: How to retrieve a random row or multiple random rows?
Pull 5 random records from mysql database
I have a large list of domains in my database in the table "list" under the column "website".
There are about 140 million rows and I just need to know how to echo a random one.
Basically like this:
<?php include('directory/database.php'); ?>
WHATEVER CODE WORKS FOR GETTING A RANDOM DOMAIN FROM THE DATABASE
<?php
echo $domain;
?>
There's just a huge amount of records so I need to know the quickest way to just have one of them randomly selected and displayed on a page. Thanks!

You definitely don't want to use ORDER BY RAND(). MySQL has to build a temporary table. If your table has a unique id column, something like this is much better:
SELECT * FROM `table`
WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` )
ORDER BY id LIMIT 1;
See this blog post for other approaches that work well in php.
Note: This simply repeats my answer on this thread.

Related

How to get 50 unique records randomly from a postgresql table? [duplicate]

This question already has answers here:
Best way to select random rows PostgreSQL
(13 answers)
Closed 3 years ago.
Scenario:
Whole number addition questions in number 1000 are generated randomly and inserted into column question with unique constraint of postgresql table questions, around 150 questions duplicate for which insertions fail and their sequence numbers are wasted.
Requirement:
When user takes test 50 unique questions has to be selected.
I tried in php as follows:
$qid=mt_rand(1, 1000);
$dmq="SELECT * FROM questions WHERE qstn_id =". $qid;
But, I am getting a) skipped question_id or b) duplicate question_ids.
Please guide me in selecting 50 unique questions randomly from the table.
You can do that using query like:
select * from YOUR_TABLE order by random() limit 50;

How to get random tuples from database? [duplicate]

This question already has answers here:
How to randomly select rows in SQL?
(13 answers)
Closed 3 years ago.
I'm working on creating a online quiz program. I want to change questions every time new user logging in. Is there any way to get questions from database randomly?
I have tried rand() function but it generates same question again and again.
You can use NEWID() of MySQL. The SQL SELECT NEWID() function returns the random row. GUID in memory for each row. By definition, the GUID is unique and random so, you will get the perfect random record.
Example:
SELECT * FROM Table ORDER BY NEWID()
TO know more you can click here

Selecting random images using php and mysql [duplicate]

This question already has answers here:
How to randomly retrieve images from my mysql database?
(3 answers)
Closed 7 years ago.
I have a table containing images path it is like this
id i_id img date
1 0 im.png 2015-05-12
2 0 im1.png 2015-05-12
3 0 im2.png 2015-05-12
And i am trying to echo out images using an src tag in php but what i need to do is that i need to echo out images in random manner and the table will grow also how to do this?. is it a php part or mysql part?.
There are two options. You can either do an ORDER BY RAND(), or you can do a select all and randomize the selected rows in PHP.
Option 1:
SELECT * from IMAGES ORDER BY RAND();
Option 2:
//select images (example uses mysql_ library; it's deprecated so DON'T USE IT!)
$query = "SELECT * FROM IMAGES";
$result = mysql_query($connection, $query)
$rows = mysql_fetch_all($result);
//randomize the array of images
shuffle($rows);
//output the images here
Option 1 can get very slow with large datasets. In my experience, Option 2 is faster, but it would be worth doing your own tests to see what works best for you. Usually the difference will be only fractions of a second. Check the answers here for more information: MySQL: Alternatives to ORDER BY RAND() or here: How can i optimize MySQL's ORDER BY RAND() function?
A third option is to generate random ids and select the albums that match those ids. This is only suitable if you don't have gaps in your ids. I have never tried this.

how to get Unique Random number in php

I am trying to develop a Computer Base Test (CBT) software on PHP.
I want it to retrieve question from MYSQL DATABASE randomly where I have 100 questions in the database.
But using <? rand(1,100); ?> there is real assurance that it may select same question at same session.
How do I select a unique random number in a session?
You can use RAND() to get random questions from database. And to take-care of uniqueness of questions you can use IN keyword while selecting new question from database. Means you need to keep track of all old Questions to check IN.
OR you can get all unique questions from database using RAND() at once and use those questions for that session.
I don't know this solves your problem with php. But with Mysql rand() you can retrieve random questions from db.
SELECT * FROM yourtable ORDER BY RAND();
If you want 10 random rows out of 100 rows
SELECT * FROM yourtable ORDER BY RAND() LIMIT 10;

different between limit query in mysql and php [duplicate]

This question already has answers here:
How to count all rows when using SELECT with LIMIT in MySQL query?
(5 answers)
Closed 9 years ago.
I'm trying to make page number for my table and need to know row count.
for example my query is SELECT * FROM tbl and the result is 100rows.
I should limit query to 10rows for one page.
if I do that I cant get all rows count.
I want to know is there any different between limit with mysql our php in process speed.
please tell me if you have any idea.
You shoud use LIMIT in your MySQL. Why?
*Because it will take you less time to fetch some amount of data and transmit it instead of getting everything.
*Because it will consume less memory.
*Because working with smaller arrays is generally faster.
As for me, I used PHP as a filter only when I could not perform the filtering in MySQL
Just to update my answer, as Joachim Isaksson already posted:
SQL_CALC_FOUND_ROWS will help you count all the rows, so you can perform a correct pagination.
The best way is probably to use FOUND_ROWS() and combine them both;
SELECT SQL_CALC_FOUND_ROWS * FROM tbl LIMIT 10
...to get the first 10 rows as a result. Then you can just do;
SELECT FOUND_ROWS()
...to get the total number of rows you would have got if you hadn't used LIMIT.
What happens if you will have 100k rows? Of course, you will get rows count, but performance significantly decrease (and memory usage significantly increase). Just use second query to obtain amount of rows and limit rows in mysql
As i understand your problem ,solution might be like this :Strictly by mysql
Use two function first for counting total record
second for getting result
select count(1) as total from table_name
select col1,col2.. from table_name limit $start , $slot_size
Now slot_size is number of record you want to show on page,say 10
start is value which will change according to page

Categories