简体   繁体   中英

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.

Another table multi_choice_pool, which contains idQuestion, answer_all.

Every answer.userEmailAddress has multiple entries of idQuestion and 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.

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.

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.

But you need the JOIN one :

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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