I have a list of clickable questions from which I'm looking to open in a new page and also display a list of options to select from a bit like a quiz or test.
So lets say in the database TableQuestions has the questions saved and TableAnswers has the possible options saved. The Answers table also includes the ID from the Questions table to determine what options are associated with each question.
However when I click the on a question it is displaying all the questions and all the answers from the database where I just want to display the question that I have clicked and the answers which have the same ID as the question.
This is my SELECT statement:
("SELECT tableQuestions.*, tableAnswers.* FROM tableQuestions, tableAnswers WHERE tableQuestions.question_id=tableAnswers.question_id");
Can anyone assist with this?
Thanks
Seems like you forgot to add the Id of clicked question in the SQL, so you get all questions.
Also, what you do here (brining data from 2 tables) is called joining tables, and the better syntax for this is with JOIN ... ON in the FROM part.
SELECT tableQuestions.*, tableAnswers.* FROM tableQuestions INNER JOIN tableAnswers
ON tableQuestions.question_id=tableAnswers.question_id
WHERE tableQuestions.question_id = <id>
As addition to Yossi's answer: You should use LEFT JOIN, insdead of INNER JOIN. This will allow you to display the questions which don't have answers.
Related
I can't figure whether to use JOIN or UNION when trying to set this up, but when I found this answer: MySQL Select all columns from one table and some from another table I thought this might work. Turns out I still get an error. Is there something I am doing wrong with setup of this?
$sql = "SELECT user_images.*, users.profile";
Basically I have user images (user_images) in one table and I also want to display some user information such as the profile column in users table.
If you get an error please include it in your question. It's relevant information and you know we're going to ask what it was.
But this is a sample query for what you want to do.
SELECT user_images.*, users.profile FROM user_images JOIN users ON user_images.id = users.id;
Joins need a linking column to work correctly, something that is the same for both rows in both tables. I've used id as an example but it may not be correct for your specific situation.
Please I need help with this problem I'm facing. I'm building an examination system and I'm using the Rand() function to select questions from the "question" table. The user's answers are also saved in the "user_answer" table.
Now my problem is a question sometimes gets selected twice or thrice so I need a query that will check that if a question has already been answered in the "user_answer" table, it should reselect another question from the "question" table.
You cannot exclude with Rand() directly.
If you query from a database, you could add something like
select *
from question q
left join user_answer ua on ua.question_id = q.id
where ua.id is null
group by q.id
This will try to connect to an answer (ANY answer, you probably want to add some user selection into that), and only give back the questions where it FAILS (ua.id is null) to do so.
If you cannot do it by query and have it it all in some PHP array, what you could do is keep track of the available question ID's in an array. Each time you pick a question random, you remove that item (value!) from the array, and reindex the array (keeping the values, which are the question ID's, and ordering the keys from 0 to the number of questions - 1).
That way you can do a rand(0, count($questionIds)) again to pick the next one.
Another way would be to use a loop, and continue as long as the picked question is already in the used questions array.
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
This seems to be a bit out of my league and knowledge of MySQL (I'm not sure if it's even possible with MySQL). So if someone can help it would be much appreciated.
I would like to do the following:
Enter/define a keyword e.g. mercedes
Find all Joomla K2 articles that have that keyword in the title
Than to each of those articles assign my keyword mercedes as a tag.
Now... There are three tables with relevant columns listed:
k2_items
id, title
k2_tags
id, name, published (value 1 is assigned if tag is published)
k2_tags_xref
id, tagID, itemID
So, query should select all items from k2_items table that have keyword in their title, check if keyword is already defined as tag in k2_tags, if not than create a new tag. After that, new k2_tags_xref entry should be generated to connect keyword tag with K2 article item.
I still didn't have database course on my university so I'm kind of out of my league with this one, and it was supposed to be just a simple touchup for the site I'm developing.
Any help with this would be much appreciated, and I'm sure it'll help community later on.
Thanks!
The part in SQL is actually pretty easy. It is simplest if you have a unique index on k2_tags_xref(tagId, itemId). This has the database check for duplicates.
Then you need to do two things:
Find all items with the keyword in the title
Find the tagId for the keyword
This results in a query like this:
insert into k2_tags_xref(tagId, itemId)
select t.tagId, i.itemId
from k2_items i cross join
(select t.tagId from k2_tags where tag = 'mercedes') t
where i.title like '%mercedes%';
You will also need to put the tag into the tags table, if it is not already there. But the above query is the basis of the SQL code.
I am creating a MCQ quiz based on php and mysql. Here are the structures of my main tables:
quiz table: quiz id, quiz_category
category table: id, title...
questions table: id, quiz id, categoryid, title...
answers table: id, question id...
To start things, I have the tables populated with 150+ quizzes, 4 categories, 14000+ questions and rightanswers for each.
To save time, for each question, the right answer is pulled from the answers table https://stackoverflow.com/editing-helpalongwith 3 other random answers .
Now when I was testing it with just two quizzes, it worked fine. But with 150 quizzes, several problems have cropped up:
the database is slow and for later quizzes takes forever to load questions
the randomization of answers is not working anymore - along with the right answer, the other options show the same entry, making it easy for the user to guess the right answer.
You can see the code I am working with in my previous Stackoverflow query. https://stackoverflow.com/questions/14826573/randomising-questions-and-answers-php-quiz-not-working
Any idea about what the ideal queries should be for the quiz program to work?
I will provide some tips on how to improve performance, however these will be generic and may not be complete.
From briefly looking at your PHP and SQL statements from your previous question, there are a few logical places for an index. To add an index please reefer to the MySQL manual for more information
$sql4="select * from answers where question_id=".$row2['id'];
question_id should have an index
$sql2="select * from questions where quiz_id=".$_SESSION['quizid'];
quiz_id should have an index
Adding these two indexes will also improve selectivity on this
$sql3="select * from answers where question_id in (select id from
questions where quiz_id =$row2[quiz_id]) order by rand()";
This will help as previously you would have been performing a full table scan for each query.
Your other issue is that you have a loop and on each iteration you are sending commands to query the database, you should collect all the information at once before the loop and then iterate using that rather than sending individual queries each iteration.
I want to make 15 questions, and any time user visits the page it shows random 5 questions and each question has 4 answers and 1 is the correct. The marks are 20, 15, 10 and 0.
How can i make it?
I always find it best to start learning by Googling for tutorials.
Here are a few:
simple php quiz
select random list items in php
I'm sorry to tell you go Googling, but I think the tutorials and examples you'll find there, will be far more helpful to you than any answer here on StackOverflow.
Ok well nobody is going to write the full code for you I think, but as a general schema design, you'd want something like this...
questions table (q_id, q_question)
questionoptions (qo_optionid, qo_questionid, qa_option)
useranswers (ua_userid, ua_questionid, ua_optionid)
To get the choices for a given question (question 1 lets say)
select
*
from
questions
inner join questionoptions on (qo_questionid = q_id)
order by
qo_optionid
To get a report of the options each user chose...
select
*
from
questions
inner join questionoptions on (qo_questionid = q_id)
inner join useranswers on (ua_questionid = q_id and ua_optionid = qo_optionid)
order by
ua_userid, ua_questionid
Note that im not advocating the use of SELECT *, but its there for simplicity of example.