Inner Join + Where in Mysql

Asked

Viewed 272 times

0

How to use INNER JOIN with WHERE?

I’m making a INNER JOIN with 3 tables. I need a WHERE to return only one record. I am trying as follows:

SELECT `*` FROM (`tabela1`) 
INNER JOIN `tabela2` ON `tabela1`.`id` = `tabela2`.`id` 
INNER JOIN `tabela3` ON `tabela2`.`id` = `tabela3`.`id` 
WHERE `tabela1`.`id` = 12;

Only Mysql returns this error:

Column 'id_militar' in Where clause is ambiguous

I want to receive only one record on INNER JOIN, not all of the table. How do I?

  • 1

    You really put nome_tabela.id_militar on Where? Because this error presented means that there is id_militar in more than one of the selected tables and he does not know which one you are comparing.

  • Are you specifying from which table is the "id_military" field? Because it seems to exist in more than one table and you’re not specifying which one it is.

  • Exactly what you said, I was making two comparisons with the same value (table.id). Thanks, problems solved

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.