How to create a query for two DBs, I have a server and there are two DBs on it, I will need to compare the value of the fields, for example, from db1.table1.column1 == db2.table1.column1
- onewell, just write and select * from db1.t1 A join db2.t1 B where A.col1 = B.col1. A user connecting to the database should have rights to both bases - Mike
- $ user = 'partsuser'; $ pass = 'pas'; $ dbh = new PDO ('mysql: host = localhost; dbname = db; charset = utf8', $ user, $ pass, array (PDO :: ATTR_PERSISTENT => true, PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION, PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC)); $ stmt = $ dbh-> query ($ sql); that is, I vseravno that I chose the db base here, I can still contact the rest? - Alexey Balyuk
- @Mike You should make your comment as an answer - Naumov
- everything works really, I thought with PDO it would be something else, thanks! - Alexey Balyuk
- If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦
|
2 answers
Most DBMSs support separate databases (you did not specify which one you have in the question) there is a default database, this one which is specified in the connection string or later explicitly selected (for example, use db
for mysql). The tables of the selected database are available for direct access.
In addition, before the table name, you can explicitly specify the database in which this table is located:
select * from db1.t1 A join db2.t1 B on A.col1=B.col1
The main thing is that the user under which you are connected has rights to the tables of both databases.
|
For sql server, if the databases on one server:
SELECT * FROM db1.dbo.t1 A JOIN db2.dbo.t2 B ON A.col1=B.col1
Where
db1, db2 - имена баз данных. dbo - схема по умолчанию t1 - имя таблиц
if the databases are on different servers, you can use linked sever for cross-server queries.
- using distinct is not possible? - Alexey Balyuk
|