Access data from two databases of two different tables in a query

Asked

Viewed 54 times

-1

Guys, I’m having a problem. Currently I have two databases and two tables (1 table in each database) that need to be connected in one query only. It would be a JOIN in other words, but I’m not getting to understand how I do it in another database.

Connection between banks has the same user and password, but has different calls.

Banco Financeiro = $financeiro->query();

Banco Clientes/Site = $site->query();

I tried to apply a JOIN and some other results I found on Google but I was not successful when I need to access another bank.

We currently have the field cliente in the table of financeiro and the countryside chns on the table of clientes, where the chns is the unique client code that is used throughout the rest of the system for identification.

Wanted SQL in this style (if possible) that I already use in connections within the same database:

$financeiro->query("SELECT * FROM financeiro AS FA JOIN clientes AS CL ON FA.cliente = CL.chns ORDER BY CL.nome ASC");

The connections are all made in PDO.

  • What was the query what you tried to do but it didn’t work? Edit your question and add it.

  • Databases are on the same server or on different servers?

1 answer

1


Simple as that:

SELECT * FROM database1.table1 tbl_1
JOIN database2.table2 tbl_2 on tbl_1.ID = tbl_2.ID

Browser other questions tagged

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