I have Two Table One to store Questions and Other to store Replies to Questions as Below
I have Shown the Table Structure and Column in table as Below
Question Table
Question_Id(PK) | Question | Name | EmailAddress
Answer Table
Answer_Id | Question_Id | Question | Name | EmailAddress
What ever question is posted it will be added to Question table and What ever Replies people post will be added to answer table
Now when ever Some one post a Reply to Question I Should Send mail to one who Posted Question and to those who posted Replies to the Question
Please Suggest a mysql Query for the above
Thank you
In theory you should know in the application the id of the question (qid) someone is replying to. Based on this id you can issue the following query:
Select EmailAddress from Question where Question_Id=qid
Union
Select EmailAddress from answer where Question_id=qid
Depending on the logic of your application this might also select the address of the current user. If you want to avoid this you should include in both select statments a condition to exclude the current replier. Something like:
Select EmailAddress from Question where Question_Id=qid and EmailAddress!=curentUserAddress
Union
Select EmailAddress from answer where Question_id=qid and EmailAddress!=curentUserAddress
Select *
from questions
left join answers
on questions.id = answers.question_id
where question_id = 1
select * from Question q, Answer a
where q.Question_Id = a.Question_id and
q.Question_Id = question_id
Related
I am working on a table that is similar to stack overflow. It stores questions answers and comments.
I tried to create a query that retrieves data in the following way. A question and its comments and answer for this question and comments for the answer.
I don't know how to retrieve all this information from the same table. If they were in different tables, it would be easy. I tried inner join and using sub-query but they didn't give me the correct output.
I want to use this query in spring-data-jpa. First, I tried to make it with jpa but it doesn't work. I think this query is more complex to create it directly with spring-data-jpa.
these are the important fields of my table in MySQL:
id | content_id | content_type_id | subject | body | tags | user_id | dtype
content_id is using for answers and comments and shows the id of a question that answer or comment belongs to. dtype specify the type of record it has one of the Question, Answer or Comment values.
these are queries that I tried:
select distinct T1.`content_id`,
(Select `body`,`dtype` from content As T3 where T3.content_id = 19 and
T3.content_type_id = 2),
(Select `body`,`dtype` from content As T4 where T4.content_type_id = 3 and
T4.content_id = T3.id)
from content as T1
SELECT a.body, a.user_id, a.dtype, c.body, c.user_id, c.dtype FROM content a
JOIN content c ON a.id = c.content_id WHERE a.content_id = 19
this is the method that I tried to use in spring boot
public Question getQuestionAnswerAndComment(Question question){
List<List> result = new ArrayList<>();
Optional<Question> ques = getQuestion(question); // getting one question by id
result.add(questionRepository.findByContentTypeIdAndContentId(3, question.getId())); //getting comments of the question (3 means comment)
Answer ans = questionRepository.findByContentTypeIdAndContentIdAnswer(2, question.getId()); // getting answers of the question (2 measn answer)
}
the problem here is the third method call that selects all the answers for a question and return a list so I can't use its output to find comments for a specific answer.
now what should I do to achieve my desired data from database, I should use spring-data-jpa methods or create a custom query and then call it.
Desired result:
Question
----Comments of the question
The first answer to the question
----Comments of the first answer
The second answer to the question
----Comments of the second answer
I would start with a select like the following which should select all the data that belongs to a question along with it in consecutive rows.
select * from content as questions
left join content as comment_or_answer
on questions.id = comment_or_answer.content_id
left join content as answer_comments
on comment_or_answer.id = answer_comments.content_id
where questions.content_type_id = <question-type-id>
and <add constraints on the question here, e.g. on the id>
order by questions.id, comment_or_answer.content_type_id, comment_or_answer.id
This gives you a threefold join of the table with questions in the first set of columns, comment and answers in the second set of columns and comments to answers in the third set of columns.
I then would execute that query using a NamedParameterJdbcTemplate and use a ResultSetExtractor to construct a result object from it.
I have 3 tables:
Subjects:
ID_subject Subject_name
Questions:
ID_question Question ID_subject
Answers
ID_answer Answer ID_question
Is it possible to make a selection where the result will be all the answers from 3rd subject? I tried to make a double JOIN but that did not work, it returned me all the answers.
This should return the results you're looking for:
SELECT a.ID_answer, a.Answer FROM Answers a
JOIN Questions q ON q.ID_question = a.ID_question
JOIN Subjects s ON s.ID_subject = q.ID_subject
WHERE s.ID_subject = ID_of_third_subject
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 need to make a database that should hold 50 questions, 3 possible answers for each question and value of each answer.
This is my first guess but it feels wrong and I'm not sure how to access this values properly.
Any suggestions on how to structure this?
This seems like correct way:
questions table
---------------
id
title
...
answers table
-------------
id
question_id
answer
value
One way would be
questions table
---------------
id
title
...
values table
------------
id
value
answers table
-------------
id
question_id
answer
value_id
Then to get all answers of a specific question do
select a.*, v.value
from answers a
join questions q on q.id = a.question_id
join values v on v.id = a.value_id
where q.title = 'what is the name of Dr. Who'
questions
----------
id
title
answers
--------
id
question_id
answer
value
This query would give you all the questions with their possible answers, with the answers listed from best to worst (assuming value is a "points" system, and not a description of quality like "Good" "Better" "Best").
SELECT *
FROM questions AS qTbl
LEFT JOIN answers AS aTbl ON qTbl.id = aTbl.question_id
ORDER BY qTbl.title, aTbl.value DESC
;
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.