Show a random mysql row, but not the same twice [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to create a database that holds all my information with questions, users, answers etc.
I've been thinking all day how to solve this problem of mine. Okey, so the problem is like this:
Let's say I have a table in my DB, with questions (300++) and the user that logs on will get a random question shown. But never the same question again, so I'll need to store this information in a separate table with the user-ID and the question-ID. And I'll need to create another table that stores the answers to the questions.
So how would this PHP/MYSQL-code look like? Because I'll need to find a random question that hasnt been shown to the same user again.
If something is unclear, please let me know. And thanks in advance

You can use RAND() function and NOT IN
Example:
SELECT * FROM `questions` WHERE id NOT IN (5, 3) ORDER BY RAND() LIMIT 1;
You have to fetch question id using sub query
Example
SELECT * FROM `questions` WHERE id NOT IN (SELECT question_id FROM shown_questions WHERE user_id=1) ORDER BY RAND() LIMIT 1;

You can use an outer join to select items that have not been used already. It might be a bit faster than NOT IN if you set up the indexes:
SELECT questions.* FROM questions
LEFT JOIN shown_questions
ON (questions.id=show_questions.question AND user_id=42)
WHERE shown_questions.question IS NULL
ORDER BY RAND() LIMIT 1;

Related

how to get a unique record from select query in mysql each time [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to fetch random record from the table but repeated game id record is will become out only once at a time. I am sending a screenshot of my table
You need to use MySQL RAND() function.
Query:
SELECT * FROM lessons_game ORDER BY RAND() LIMIT 1
// Put proper fields instead of *
Explanation:
ORDER BY RAND() will shuffle records random records from DB Table.
And we will get random records after we execute the query result set.
We are putting LIMIT 1 so only one record will be pulled out.
Try the following command
SELECT * FROM lessons_game ORDER BY RAND() LIMIT 1

how to make this mysql query fast? I have millions of data inside my table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a table called ad_view and it has 18.9 million number of data inside that table.
This is the table design of ad_vew
ad_view
-------
ad_id
network_id
publisher_id
landingpage_id
advertiser_id
I am using this query to get the ad_id and the count of ad_id depending on the network_id, so in this case the network_id is 4.
select ad_id,count(ad_id) as strength
from ad_view
where network_id = 4
group by ad_id
order by strength desc
limit 10
This query takes forever to load. Do you have any suggestion how to make this query fast?
I am not a database expert, if it takes to redesign this table, I would do that.
Any suggestion will be greatly appreciated, thanks!
This will help:
ALTER TABLE ad_view ADD INDEX (network_id, ad_id).
Make sure you have tuned your innodb_buffer_pool_size to hold the frequently-requested portion of your table in memory.
You might like my presentation, How to Design Indexes, Really. That presentation doesn't go into indexing for GROUP BY, but you can think of it like a range condition.
1 Create an index on network_id since you're searching by it
ALTER TABLE `ad_view` ADD INDEX (`network_id`);
2 If you're trying to get the count of ad_id for a given network_id, why do you need ad_id in your results? and why do you need to order by? I don't understand this. If all you want is how many ad_ids for network_id 4, then do:
SELECT COUNT(IF(network_id=4,1,null)) as strength from ad_view
It will return just a number. See this demo
PS: your initial post included a broken query (in the order by clause), which you changed after I made a comment. Your current query still doesn't give you what you say you want. I just tried it on this fiddle

Select and sort data from one table on certain columns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm looking to see if it is possible to select data from table A with a sort so the order of the data changes.
I then want to make a loop so it will go through each row and add it into table B.
Is this possible.
Use a variable #row_number to store the incremented num for your expected sorting.
SET #row_number = 0;
SELECT (#row_number:=#row_number + 1) AS num, Column1
FROM TableA
ORDER BY Column1
This result can be store in temporary table and based on that you can do loop/insert into the TableB

How to count certain rows in a MySQL table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I currently have a table that looks like this.
http://i.stack.imgur.com/KFP6Q.png
This is a comment system. the column id gives the comment an ID. the server_id is the ID for the section the comment was posted on. The user_id is the ID for the person who posted it. And lastly, the comment is the comment itself. Here is how I created the comment:
http://pastebin.com/VHUDW6Dm
What I want to do is create a variable, $commentcount, that will count how many comments there are for a server and be able to display them on a page. If someone could direct me to a function that can help me with this or actually create the code here, it would be greatly appreciated.
Since you want the number of comments per server, you can use the SQL GROUP BY clause to aggregate the resulting rows by the unique server_id.
SELECT server_id, COUNT(id) FROM comments GROUP BY server_id;
This will return the count for each server_id group. If you are only displaying this for a single server_id at a time, you can simply use
SELECT COUNT(id) FROM comments WHERE server_id = <your server id>;
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
A very rough draft for you would be to iterate a count.
$q = mysql_query("MYSQL QUERY TO GET ALL THE COMMENTS FOR THE USER");
$count = 1; // Start the count, preferrably at 1
while ($comment = mysql_fetch_assoc($q)) {
$count++; //iterate the count
}
echo $count; // Echo's the count;
use following mysql query
select count(*) as count_comment from comments;

assesment for php application code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Thanks for reading.... still on my way to really getting the hang of php an MySQL queries.
I have a system that queries table entries... I have managed the joining of the 2 tables in my sql query.
Sample:
$sql = mysql_query("select * from table1,table2 where `field_table1` = `field_table2` and `x`= 'x' and y = 'y'")
My question on the validation for this query... The joined field in table1 has to be the same as field in table2. Is an if statement the solution and how would I approach this?
Basically I do not want the query to show the result of the search if field_table1 is not = to field_table2.
Thank you for your assistance.
SELECT *
FROM table1
INNER JOIN table2 ON...
WHERE table1.field != table2.field
That should pretty much take care of it.

Categories