It is the same syntax as the query of two tables in the same database. First here I'll leave you a little information about the JOIN
s. Now, imagine that you have the databases baseDatos1
and baseDatos2
, both with your scheme dbo
. Now in the first there is a table tblbd1
and in the second there is a table called tblbd2
. Let's suppose that the two tables are in the schemas dbo
of their respective databases. The query would be as follows:
SELECT * FROM baseDatos1.dbo.tblbd1
INNER JOIN baseDatos2.dbo.tblbd2
ON baseDatos1.tblbd1.campo = baseDatos2.tblbd2.campo
Now if you have more than one condition you just have to add a AND
after the first one
SELECT * FROM baseDatos1.dbo.tblbd1
INNER JOIN baseDatos2.dbo.tblbd2
ON baseDatos1.tblbd1.campo = baseDatos2.tblbd2.campo
AND baseDatos1.tblbd1.fecha = baseDatos2.tblbd2.fecha
AND baseDatos1.tblbd1.id_campo = baseDatos2.tblbd2.id_campo
To select certain fields you just have to specify the field and which table it is from
SELECT t1.fecha, t2.id, t1.estatus FROM baseDatos1.dbo.tblbd1 t1
INNER JOIN baseDatos2.dbo.tblbd2 t2
ON t1.campo = t2.campo
Note: In this last query I use a alias to that the query is simplified