2012-07-13 66 views
3

我有连接查询的结果二表一储存问题和其他到答复的问题存储如下mysql的两个表

我已经展示了在表中的表结构和列如下

问题表

 
Question_Id(PK) | Question | Name | EmailAddress 

回答表

 
Answer_Id | Question_Id | Question | Name | EmailAddress    

什么都问题是贴吧将被添加到问题表,什么都回复人岗将被加入到回答表

现在,当曾经有人发表回复的问题,我应该将邮件发送到一个发布谁问及那些谁已发表回复的问题

请建议对上述

MySQL查询谢谢

回答

1

从理论上讲,你应该在应用程序知道问题的ID(QID)有人回答。在此基础上的ID,你可以发出以下查询:

Select EmailAddress from Question where Question_Id=qid 
Union 
Select EmailAddress from answer where Question_id=qid 

根据应用程序的逻辑,这也可能会选择当前用户的地址。如果你想避免这种情况,你应该在两个选择的条件中包含一个条件来排除当前的回复。喜欢的东西:

Select EmailAddress from Question where Question_Id=qid and EmailAddress!=curentUserAddress 
Union 
Select EmailAddress from answer where Question_id=qid and EmailAddress!=curentUserAddress 
0
Select * 
from questions 
left join answers 
on questions.id = answers.question_id 
where question_id = 1 
0
select * from Question q, Answer a 
where q.Question_Id = a.Question_id and 
     q.Question_Id = question_id