INNER JOIN in 2 tables from 2 different databases

Asked

Viewed 42 times

-2

Good afternoon,

I need to make an INNER JOIN between 2 tables where each one is in a different database, but on the same server.

I have 1 table called "settings" and others called "customer launches" the column with is the "id" which is the primary key of settings.

I’ve searched a lot around here, but I can’t find a solution, or I don’t understand how it works.

I tried this and it didn’t work:

  $sql01= ("SELECT * FROM $db1.definicao INNER JOIN $db2.lancamento_clientes ON $db1.definicao_variaveis.id = $db2.lancamento_clientes.id WHERE definicao_variaveis.id00 = '$id00' AND definicao_variaveis.nivel = '1' AND $id_tabela_base = $id_referencia_base ORDER BY definicao_variaveis.ordem");
while($linha01 = $sql01->fetch_assoc()) {echo $linha01['id'];}

But it didn’t work out, which I’m doing wrong, so somebody can help please. Thank you

1 answer

1


Assuming the user used has access to the tables, it can be done

SELECT * FROM <DB1>.definicao_variaveis INNER JOIN <DB2>.lancamentos_clientes ON ...

Where DB1 and DB2 are the names of Databases.

  • Thank you. I already understood thank you, I thought DB1 and DB2 were the variables of connection to the Database.

Browser other questions tagged

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