Doubt about the use of JOIN...

Asked

Viewed 43 times

1

I wonder if the field in which I should make the comparison, can only be the one that contains the key Primary, or can be any field...

Let me give you an example:

 CREATE TABLE tabela1(
    idTabela1 INTEGER NOT NULL AUTO_INCREMENT,
    nome1 VARCHAR(50) NOT NULL,
    PRIMARY KEY(idTabela1)
    )Engine InnoDB;


 CREATE TABLE tabela2(
    idTabela2 INTEGER NOT NULL AUTO_INCREMENT,
    nome2 VARCHAR(50) NOT NULL,
    PRIMARY KEY(idTabela2)
    FOREIGN KEY(idTabela2) REFERENCES tabela1(idTabela1)
    )Engine InnoDB;


 SELECT * FROM tabela1 AS t1 JOIN tabela2 AS t2 ON t1.nome1 = t2.nome2;

I can do it ?

  • Hello. You can use any field. See this example on Sqlfiddle.

  • Thank you very much.

  • There is something wrong, being table2.idtabela2 autoincrement there is no way to guarantee it is FK from another table.

No answers

Browser other questions tagged

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