简体   繁体   English

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

[英]Comparing two columns in two different tables?

First of all apologies for if the syntax and framing of question isn't up to the standards. 首先道歉,如果问题的语法和框架不符合标准。

I have a MySql database .I have a table answer which contains idquestion, userAnswer, userEmailAddress as columns. 我有一个MySql数据库。我有一个表答案,其中包含idquestion,userAnswer,userEmailAddress作为列。

Another table multi_choice_pool, which contains idQuestion, answer_all. 另一个表multi_choice_pool,其中包含idQuestion,answer_all。

Every answer.userEmailAddress has multiple entries of idQuestion and userAnswer. 每个answer.userEmailAddress都有idQuestion和userAnswer的多个条目。

I want to obtain userEmailAddress in answer table where id and answer of that userEmailAddress equals the iq and answer of multi_choice_pool. 我想在答案表中获取userEmailAddress,其中该userEmailAddress的ID和答案等于multi_choice_pool的iq和答案。

I wrote this: 我这样写:

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

Which is giving me an error: "Unknown column 'multi_choice_pool' in where clause. 这给我一个错误:“ where子句中的未知列'multi_choice_pool'。

Is the syntax wrong? 语法错误吗? Or the query is wrong itself? 还是查询本身是错误的? Or my approach isn't right? 还是我的方法不对? Can you rectify and provide suggestion? 您可以纠正并提供建议吗?

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;

It seems you don't need the WHERE clause. 似乎您不需要WHERE子句。

But you need the JOIN one : 但是您需要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

Here is the MySQL documentation for JOIN . 这是JOINMySQL文档

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

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