How to get random tuples from database? [duplicate] - php

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

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 select n random, not match rows form database? and data saving to session [duplicate]

This question already has answers here:
MySQL select 10 random rows from 600K rows fast
(28 answers)
Closed 6 years ago.
Is it better to select random data with pure SQL, or just select entire table and shuffle it? If pure SQL is better could someone show me an example of the most efficient way of doing it?
Also is it a good idea to save this selected data into a session variable? I want to make quiz with one question per page and with ability to get back to answered questions and change its answer. The session would save a double array of questions and answers to them that user picked. Or maybe there is a more efficient way of doing this?
Definitely, it's better to select random data with pure SQL. Here you can read how to select random rows in mysql.
You can save this data on the server side (SESSION) or on the client side (COOKIE). But it's not principal in this case. Most importantly, that the application should have access to user questions and answers after the last question
The "Order by rand()" function in MySQL is really slow and should usually not be used. Either shuffle after selection or, preferably, randomize a row reference and then do your query selection. The latter alternative is the most efficient one if it can be used.
SELECT bar, foo, rand() FROM tabname ORDER BY 3 LIMIT 0,1;
Of course you can use AS and so own and the LIMIT is just important if you wanna output some single data ( like questions for some game ).
P.S - Some people will say that rand() is to slowly, but that's not true, it just depends how you and where you will use it. In this simple way above, it is pretty fast and if you making a quiz which has under 150 000 questions :-), it is just ok.
Hope i could help you. Cheers.

Retrieve last inserted id based on column PHP [duplicate]

This question already has answers here:
Get last insert id after a prepared insert with PDO
(3 answers)
Closed 7 years ago.
I'm looking to be able to retrieve the last inserted id based on username. I know within PDO I am able to use to use lastInsertId() but from what I can tell that only gets the last one inserted with no other parameters. Is this possible in a short and simple?
The only way I thought I could do this was using something like
SELECT id FROM table_name WHERE username=:username ORDER BY enteredTimenDate ASC
Would this be the easiest way I could do this?
EDIT to address duplicate issue. The "Original" question this question is considered a duplicate of just asks how to retrieve the last inserted ID, my question was retrieving the last inserted ID based on another parameter.
Use following query to get the last inserted id:
SELECT id FROM table_name WHERE username=:username ORDER BY enteredTimenDate desc limit 1;
Data is sorted based on the enteredTimenDate in descending order and then picking up the first row by limiting the query result.

How to echo random row from database? [duplicate]

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.

How to count and display clicks on a link [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to set cookies for uuid
Hello, my question is how to count and display clicks on a php value (since the link id changes a lot, ex. http://site.com/id=232323) . so for example, i want to display this
http://gjfkgfkgf.com/id=12345- 323 clicks
instead of this
http://gjfkgfkgf.com/id=12345
Create a MySQL table with the fields id and count
Every time the site is loaded, connect to the MySQL database, fetch the count value from the database using the id in the URL, add 1 to the count and UPDATE the MySQL row with the new count.
If you feel like being advanced you can add 1 to the count column with a single query :-)
UPDATE linkcount
SET `count`=`count`+1
WHERE `id`='12345'
Now you can fetch the number of visits to a specific link by
SELECT `count`
FROM linkcount
WHERE `id`='12345'

Categories