比较两个不同表中的两列?

时间:2014-10-13 09:03:29

标签: mysql sql

如果问题的语法和框架不符合标准,首先道歉。

我有一个MySql数据库。我有一个表格答案,其中包含idquestion,userAnswer,userEmailAddress作为列。

另一个表multi_choice_pool,其中包含idQuestion,answer_all。

每个answer.userEmailAddress都有多个idQuestion和userAnswer条目。

我想在answer表中获取userEmailAddress,其中userEmailAddress的id和answer等于multi_choice_pool的iq和answer。

我写了这个:

Select answer.userEmailAddress from answer 
where (answer.idQuestion=multi_choice_pool.idQuestion) AND  
(answer.userAnswer=multi_choice_pool.answer_all);

这给了我一个错误:"未知栏' multi_choice_pool'在where子句中。

语法错了吗?或者查询本身是错误的?或者我的做法不对?你能纠正并提出建议吗?

2 个答案:

答案 0 :(得分:0)

Select answer.userEmailAddress 
from answer left join multi_choice_pool
on answer.idQuestion = multi_choice_pool.idQuestion 
and answer.userAnswer = multi_choice_pool.answer_all;

答案 1 :(得分:0)

您似乎不需要WHERE子句。

但你需要JOIN一个:

SELECT answ.userEmailAddress 
FROM answer answ
LEFT OUTER JOIN multi_choice_pool mcp 
    ON answ.idQuestion = mcp.idQuestion
    AND answ.userAnswer = mcp.answer_all

这是MySQL documentation for JOIN