I have 3 tables (user, questions, answers) in the question table i have id_question and question, in the answer table I have id_question, user_id and answer. I want to run a query that gives me the number of answer's per question.
I want a column that has all questions that were answered (not the id_question) and 3 other columns (yes, no, maybe) that have the number of times that that answer was given to a question. The question column can't have repeated questions (even though users can answer the same questions and give different answers, I want the number of yes, nos and maybes to each question). I'm only created the yes column, having trouble in creating the other 2
Here's what I did so far:
SELECT
questions.question, COUNT(answers.answer) As Yes
FROM
answers
INNER JOIN
questions ON questions.id_question = answers.id_question
WHERE
answer = 'yes'
GROUP BY
question
ORDER BY
questions.id_question ASC
SELECT questions.question,
SUM(IF(answer='yes', 1, 0)) AS Yes,
SUM(IF(answer='no', 1, 0)) AS No,
SUM(IF(answer='maybe', 1, 0)) AS Maybe
FROM answers
INNER JOIN questions ON questions.id_question=answers.id_question
GROUP BY question
ORDER BY questions.id_question ASC
Related
My database design is like image shows:
The data table is design for a survey's all respondent's answers. The query I need is, for example,
I want to know all RID(Respondent ID) that has conditions of QID = 2 and Answers = 26-35, and QID = 4 and Answers = "ASHFIELD". But the sql query below:
select * from RespondentAnswers
where (QID = 2 and Answers = '26-35') and (QID = 4 and Answers = 'ASHFIELD')
was obvious not correct.
In short, I want to know those respondents who is age from '26-35' and living in 'ASHFIELD'.
My database structure was showed in the image above. Any one has solution please? Thank you!
could be using a group by an having
select rid
from RespondentAnswers
where (QID , Answers ) in ( (2, '26-35'), (4,'ASHFIELD'))
group by rid
having count(*) =2;
Thanks for #scaisEdge suggestion, based on it I figured out a possible correct answer, which may help other people who encountering the same issue:
select RID
from RespondentAnswers
where QID in (2,4) and Answers in ('26-35', 'ASHFIELD')
group by RID
having count(*) =2;
I already tested on many conditions and all works fine, hope this is the correct answer I need.
MySQL.
I have two tables, one is "Questions" and the other is "Answers"
The Questions table:
- question_id
- user_id
- question
The Answers table:
- answer_id
- question_id
- user_id
- answer
- correct
The goal is to get all questions (and associative answers) based on a user's id. I've been able to get all of the answers, however I'm only getting one question. I can see why it's only getting a single question, but I don't have any idea how to go about getting the question text for each answer.
Here's the code that I'm using right now. Where id_in is an input value on a saved procedure. The issue is that it gives me all of the answers for each question, but all of them return the same question text. I feel like possibly a type of join would be better here, but we haven't started learning about them yet and I hardly know anything about them as is.
BEGIN
DECLARE question_text VARCHAR(40);
SELECT question INTO question_text FROM questions WHERE user_id = id_in;
SELECT question_text, Q.* FROM answers AS Q WHERE user_id = id_in;
END
Yes, this is homework. I'm just completely lost as to what I need to be doing.
Left joins allow for All things in the left table, and only the matching things in the right table. In my example I may have A and Q mixed up but I think this is the general gist of it. You can also take the user_id = in_id and move that to a wear, but filter on the join should be faster.
SELECT
Q.QUESTION
, A.ANSWER
, A.CORRECT
FROM ANSWERS A
LEFT JOIN QUESTION Q
ON A.QUESTION_ID = Q.QUESTION_ID
AND A.USER_ID = Q.USER_ID
AND A.USER_ID = ID_IN
AND Q.USER_ID = ID_IN
I have these two table namely question and answer.
Question table
exam_id /* unique ID
ref_number /* for numbering of question */
value /* the question */
Answer table
exam_id, /*id to connect to question */
ref_number /*identifier for what question */
answer /*the value */
I used this SQL to get the fields in the database
SELECT exam_answer.*, exam_question.* FROM exam_question INNER JOIN exam_answer ON exam_question.exam_id = exam_answer.exam_id WHERE exam_question.exam_id =10
I tested using the ID 10 to get the fields, but the questions are repeating based on the number of counts of answers. Which is wrong, what I'm trying to do is to query the question and then the corresponding answer. Something like this.
Question 1
Answer 1,
answer 2,
answer 3
answer 4
Question 2
Answer 1,
answer 2,
answer 3
answer 4
Any idea on what I am missing?
While this is generally to be considered presentation logic, I have had the need to handle this using the database. Using union and creating a sort order can handle the layout.
select result
from (
select exam_id, ref_number, value as result, 1 as sort_order
from question
union all
select exam_id, ref_number, answer as result, 2 as sort_order
from answer
) t
order by exam_id, ref_number, sort_order
SQL Fiddle Demo
It looks like your are missing a predicate in the ON clause
ON exam_question.exam_id = exam_answer.exam_id
AND exam_question.ref_number = exam_answer.ref_number
Based on the information given in your question, it looks like you want ref_number column on the answer to match the ref_number column on the question.
MySQLFiddle here: http://sqlfiddle.com/#!2/15d447/1
I have a single table I am trying to work with:
Table 1: user_answers table (stores users answers to various questions)
The notable values that are stored are the users id (column uid), the question id for the question they are answering (column quid), the answer to the question (column answer) and the importance of their answer (column importance).
The end result I want:
I'd like to be able to grab all the questions that any two users have answered, excluding any answers from questions that have either not been answered by the other party, or answers to the same question which have a value of 1 for either user in importance. Again, this will only ever be used to compare two users at a time.
I've been pretty unsuccesful in my attempts, but here is what I've tried, just piecing things together:
#attempt one: trying to exclude answers that were not answered by both users
SELECT * FROM user_answers AS uid1
JOIN user_answers AS uid2 ON uid1.uid = uid2.uid
WHERE uid1.uid = 1
AND uid2.uid = 20008
AND uid1.quid IS NOT NULL
AND uid2.quid IS NOT NULL;
This returns no results but I'm not exactly sure why.
#attempt two: trying to exclude where answers are the same for both users
SELECT * FROM user_answers AS uid1
LEFT JOIN user_answers AS uid2 ON (uid1.uid = uid2.uid AND uid1.answer <> uid2.answer)
This gives me results, but seems to be doubling up on everything because of the join. I also tried in this attempt to eliminate any answers what were the same, which seems to be working in that sense.
Any guidance is appreciated.
Thanks.
You can answer your question using an aggregation query. The idea is to using the having clause to filter the rows for the conditions you are looking at.
Because you are not interested at all in questions with importance = 1 those are filtered using a where clause:
select ua.quid
from user_answers ua
where importance <> 1 and uid in (1, 20008)
group by ua.quid
having sum(uid = 1) > 0 and
sum(uid = 20008) > 0;
If you want to include the answers, you can do:
select ua.quid,
group_concat(concat(uid, ':', answer) order by uid) as answers
Just a simple version of what you need.
select *
from user_answers a,
user_answers b
where a.quid = b.quid
and a.uid <> b.uid
and 1 not in (a.importance, b.importance)
If you like to filter just the questions just change the * for distinct a.quid
See it here on fiddle: http://sqlfiddle.com/#!2/15d447/15
Hi I'm currently developing a sort of quiz application. In "Discover" section I get popular and latest questions. But when I get them I should also check if question is already answered by current user or not.
Here are my tables:
questions: (media_id is primary key, player_id is creator of the question)
media_id - player_id - answer0 - answer1 - ...
answers: (media_id is primary key, player_id is the person who answers the question)
media_id - player_id - result - ...
This is how I get popular questions:
select * from questions where order by popularity_count
EDIT:
Let me show what I want to achieve by examples:
questions rows:
media_id - player_id - answer0 - answer1
0123456 abc123 bla bla
6543210 hjk789 lor ips
answers rows:
media_id - player_id - result
6543210 abc123 1
So when user "abc123" gets the popular questions results should be:
media_id - player_id - answer0 - answer1 - is_answered
0123456 abc123 bla bla 0
6543210 hjk789 lor ips 1
The problem is I need a temporary column in the result of query that indicates if question is replied by the current user or not. How can I achieve this?
I can find if question is already answered or not for popularity section by using two queries. First I get the popular questions then I simply check if question is answered or not for every questions which seems so ineffective way. How can I achieve this with only one query?
Thank you!
It doesn't seem you are approaching this problem correctly as you have a non-normalized table structure.
You should have a separate table that contains the answers by row.
questions (id, media_id, player_id, question)
answers (id, media_id, player_id, question_id, answer)
This way you can LEFT JOIN answers to questions and easily see which questions are not answered like so:
SELECT q.question, a.answer
FROM question q
LEFT JOIN answers a ON a.question_id = q.id
AND a.media_id = q.media_id
AND a.player_id = q.player_id
See it in action
To add a column to your current query, you can append it to your SELECT and use a CASE statement to check whether it's been answered.
SELECT ...,
CASE
WHEN answer0 IS NOT NULL
THEN 'Yes'
ELSE 'No'
END AS 'Answered'
FROM ...
Update
Based on your update, it seems as though you are only JOINing on player_id. I believe you intend to JOIN on both player_id and media_id. See the difference between the data you provided and what I think you intend it to be.