I am trying to make a quiz that should show questions not answered before by the same user.
Therefore, a session is created when a user starts the quiz.
Here is the situation:
There is a table "Questions"
This table contains all questions. There is a unique field QuestionId.
The questions always have a CategoryId from 1 to 5
There is a table "Answers"
This table contains all answers given by users. The session id is stored in SessionId, the answered question id is stored in QuestionId.
I now am looking for a query that
Fetches a question from Questions
Where Category Id = X
That has not been answered before (so the QuestionId should not be listed in Answers with the same SessionId)
My own trials and some Googling did not help. Hope somebody here can help me out.
Thanks in advance!
Try this:
select
*
from
question
where
categoryid = x and
not exists (
select
'1'
from
answers
where
sessionid = y and
answers.questionid = question.questionid
)
It will list all questions that don't have an answer from the user. You can put a "limit" on the results to get back only 1 row if that's what you need.
Related
I need help building a MySQL query to query 3 tables.
I am building an online quiz with stored answers in MySQL and PHP. I am using 3 tables for the user, questions and answers. I want to provide the users with questions that they've not answered previously but can't get the query to work.
The database table structures are as follows:-
Users
user_id
language
+ other things
questions
id
question_number
text
language
answers
answer_value
id
question_number
user_id
I can obtain the answers that they have completed by using
SELECT questions.question_number, questions.text
from questions
JOIN answers ON answers.question_number=questions.question_number
JOIN users on users.user_id=answers.user_id
WHERE questions.language=users.language AND users.user_id='1'
This returns the text for the questions answered but I want to obtain the question number and text for the questions that they haven't answered. I think I've pulled all my hair out trying to solve this one. Any help greatly appreciated.
I've solved it thanks by using
SELECT DISTINCT questions.question_number, questions.text FROM questions WHERE questions.question_number NOT IN(SELECT answers.question_number FROM answers WHERE answers.user_id=$var)
I'm making a quiz game, and I want to make statistics of questions that has been answered wrong.
I have a database with 3 tables: Questions (all questions), Answers (the alternatives for each question), Games (stats about each game sessions).
Right now I'm thinking of two alternative ways to make a solution for this.
Save each question ID into an array, then save the array
into one record in MySQL at the end of the game.
Insert a new record with question ID into the table for each wrong
answer.
Which of these options would be the best approach to solve my problem? If I'm correctly, the last one will be easier when I'm gonna query the database to show questions that has been answered wrong. Any input or suggestions would be appreciated!
My advice is to save each answer given into a separate row, exactly for the reason you stated. Querying such a structure will be much easier than having multiple answers stored in a single row.
I would structure the tables as follows
Question
ID | Text | whatever else you may need
Answer
ID | QuestionID | Text | IsCorrect | whatever else you may need
Game
ID | User | StartTime | EndTime | whatever else you may need
Stats
ID | GameID | QuestionID | AnswerID | AnswerTime | whatever else you may need
This would need adjustments depending on the kind of analysis you're planning to do on it, but you get the idea.
Counting the number of times a wrong answers has been given would be as easy as
select t1.ID, count(*)
from Answer t1
join Stats t2
on t1.ID = t2.AnswerID
where t1.IsCorrect = 'N'
group by t1.ID
I am not sure I understood correctly your question. What follow is a possible solution for table statistics for answers if you want detailed information for each question / answer :
CREATE TABLE GAME_STATS (ID INT NOT NULL, ID_QUEST INT, ID_ANSWER INT, NUM INT);
ID is a sequence (you could omit this and make PK ID_QUEST, ID_ANSWER)
ID_QUEST refers to ID of table Questions
ID_ANSWER refers to ID of table Answers.
NUM is the number of times that answer has been selected.
You could consider to prepopulate the table for all possibile questions / answers (NUM = 0).
At the end of every answer, you should update the NUM (+1) for ID_QUEST, ID_ANSWERS.
If you want register statistics for each session / user, you should add appropriate columns (ex. id_session, and/or user_id).
In this case you can't prepopulate table. And you can register only wrong answers.
I'm creating a way that a "teacher" could make an exam and the "student" can take an exam.
First off, it is also possible for a teacher to make new questions, using this query:
INSERT INTO questions (question, type) VALUES ('$question', '$type')
In the database, I set questions to also have question_id which is auto incremented after each entry. Then on a separate page, they can pick which questions they would like to add to the exam. So I just:
SELECT * FROM questions
Then there is a checkbox for them to check which questions to add the use this query:
INSERT INTO exams (question_id) VALUES ('$question_id')
The table exams also has an auto incremented exam_id.
So now I would like to display the questions the teacher picked, but I don't even know what type I should store question_id in exams (right now it is INT) so I can loop through them.
ie. Teacher picks questions 1,2,4,10 and query for getting the question would look like
SELECT question FROM questions WHERE question_id='1,2,4,10'
Assuming you are getting the question id by POST or GET, Try this:
$selected = implode(',', $_REQUEST['selectedquestionids']);
SELECT question FROM questions
WHERE question_id IN ($selected)
GROUP BY question_id;
Hope this may help.
On my website I have a page where users can answer one question at a time.
The questions that are provided are stored in the questions table.
When the user answers a question it gets stored in the answers table.
After answering a question it moves on to the next question.
The way my code works right now is it selects a question from the questions table and then joins the answers table WHERE answers.id IS NULL. (the user hasn't answered this question.)
The way I need it to work is similar to above, except it needs to also check if the question has been answered by the user, and if it has don't display it and move on to the next question the user hasn't answered.
I hope I've explained myself well enough.
The code I'm currently using
$db = db_open();
$query = "SELECT
questions.id,
questions.question
FROM questions
LEFT JOIN answers ON questions.id = answers.question
WHERE answers.id IS NULL
GROUP BY questions.id";
$result = db_query($db,$query);
$result = db_fetch_array($result);
$default_question = array("id"=>$result["id"],"question"=>$result["question"]);
Question table
id question
1 This is a question
Answer table
id user question answer
1 1 1 This is an answer
Try with this:
$user_id = getUserIdOrSomethingLikeThat();
$query = "SELECT
questions.id,
questions.question
FROM questions
WHERE NOT EXISTS (
SELECT * FROM answers WHERE answers.question = questions.id AND answers.user = $user_id
)
Also consider to refactor your table definitions. A the column names are confusing. Something like this would be better:
question table fields: id, body
answer table fields: id, question_id, user_id,body
Before I ask my question, I just wanted to thank everyone that replied to my question yesterday -> Countdown using javascript.
Idea:
I created a quiz using php, but I would like to create a MySQL Database and having a table with all the questions, answers and multiple choice stored inside.
Issue:
Since the quiz is multiple choice, I don't know how to go about storing the multiple choice options in the table. Could I store the options and have each answer separated by a special character and let php get the string and separate the options?
Ex: Question: What is your favorite color? Options: Blue=Red=Purple=Yellow.(Database View)
What do you folks think is the best practice for something like this?
I think the best practice would be to use multiple tables. One for the question and one for answers. The answer table would contain question_id as well as a flag whether or not it is the correct answer
It could look like this
TABLE questions
FIELDS: id, text
TABLE answers
FIELDS: id, question_id, text, correct
The problem with using one field for all the answers is that you could accidently use the character you use for splitting inside the text of an answer
simply, create two tables:
questions
question_id, question
answers
answer_id, question_id, answer
Now, you can link these two tables using question_id
There are a couple of ways around this :
The "proper" way is to create another table ( so that you have a table called "questions", each of which have a unique id, and another called "answers", where each has the question id )
The "simple" way, which is to use JSON ( see json_encode and json_decode ) which takes care of using special characters etc in a field
You'd have a questions table, like this:
id | question
0 Do you even lift?
id would be INT(11) PRIMARY_KEY AUTO_INCREMENT while question would just be TEXT. Then, you would have an answers table:
id | question_id | answer
0 0 Yes
1 0 No
2 0 Maybe
Here, question_id refers to the ID of the question in the questions table. These answers all belong to one question. This is called a Has many relationship, as one question has many answers.
This is how its usually done. Implementing it is not that hard, even if you're not using a framework (most of them do the work for you).
Hope this helps
Tables:
quiz
quiz id (pk)
quiz info (other columns)
questions
question id(pk)
quiz id
question text
answers
answer id(pk)
question id
answer text
To display a given question do a join on the quiz, question and answers.
EDIT: You could either add a column for 'right/wrong answer (0/1)' or have another table:
solutions
solution id(pk)
question id
answer id
I didn't put the 'correct answer' in the answers table as that's not good normalization.
There could be many possible schema designs for this but my suggestion is like this:
Don't ever store values separated by comma on the tables.
This table holds the quizzes.
Quiz Table
QuizID (PK)
other columns..
This holds the questions for every quiz.
Question Table
QuestionID (PK)
QuestionDetail
QuizID (FK)
other columns...
This holds the answers.
Answer Table
AnswerID (PK)
AnswerDetail
This holds the correct answer for every question on each quiz or in other words, this is the answer key.
Question_Answer_Correct Table
QuestionID (FK) -- also a compound primary key with AnswerID
AnswerID (FK)
This contains list of users.
User Table
UserID (PK)
UserName
other columns...
This contains answer of users on a specified question. There is no QuizID here since the questions are already connected on the quiz table.
User_Answers Table
UserID (FK)
QuestionID (FK)
AnswerID (FK)