简体   繁体   English

mysql从两个表中连接查询结果

[英]mysql Join Query for Result from two table

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 请为上面的内容建议一个mysql查询

Thank you 谢谢

In theory you should know in the application the id of the question (qid) someone is replying to. 从理论上讲,你应该在应用程序中知道有人回答的问题(qid)的id。 Based on this id you can issue the following query: 根据此ID,您可以发出以下查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM