Querying database from result of other query - php

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)

Related

Show data from database based on user input

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.

Show a random mysql row, but not the same twice [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 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;

SQL for selecting rows in DB

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

Storing more than one string in a MySQL Database Table

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)

MySQL - Show questions not answered before

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.

Categories