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.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have 2 different tables. Both have several different columns, but there's a column serial_number that is in both these tables with duplicate serial numbers.
In the table 1, I want to append the row data of table 2 if the serial_number matches in both the tables.
Example:
Table 1
id, serial_number, name , phone , email
1, 87454126 , Chris , 5105487451, example#example.com
Table 2
id, serial_number, status , reason
1, 87454126 , Completed, Some Reason
The result I want:
Table 1 changes to:
1, 87454126, Chris, 5105487451, example#example.com, Completed, Some Reason
Kindly help me, I can't figure this out. Thanks
You can give both tables a different name and join like:
SELECT *
FROM Table1 AS t1
JOIN Table2 AS t2 ON t1.serial_number = t2.serial_number;
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
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table containing "ID" column.
Different numeric ids are present in this column from 1000 to 9999.
(1000, 1001.....,5000,5001....,8000,8001....,9000....9999).
Now I need a "generic" query which will give me distinct starting id series number present in this column.
for example, desired output
1000,
5000,
8000,
9000
If in future 7000 series is added in the ID column then output of the same query should include 7000 also in list like
1000,5000,7000,8000,9000
Another way to do it if your series start with every thousand
SELECT id
FROM table1
WHERE id % 1000 = 0
Here is SQLFiddle demo
Now here is a generic way which treats every "island" in id values (a sequence of values where next = prev + 1) as a series. This way a series can start and stop with any value
SELECT t1.id
FROM table1 t1 LEFT JOIN table1 t2
ON t1.id = t2.id + 1
WHERE t2.id IS NULL
Here is SQLFiddle demo
Try This
SELECT min(id) from tablename group by SUBSTRING(id, 1, 1)
try this and its for 2 zeros..
select * from table where id like '%000' or id like '%00';
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.