Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ankan600's avatar

Many to Many relation with two different database

How to create many to many relation with different database

1 like
3 replies
vincent15000's avatar

A relationship allows to add a jointure in the query in order to retrieve related data from the same database.

If you need to retrieve data from several databases, do it step by step : first from the first database, and then from the second one.

martinbean's avatar

@ankan600 You can’t. Pretty much every relational database won’t let you do foreign keys/joins across databases.

1 like
Tray2's avatar

Depends on what you mean by different databases. Is it two completely different servers, or are we talking same server two different schemas?

With two servers it will not work, but it might with different schemas, just make sure to add the schema name.

SELECT *
FROM schema1.authors,
		    schema2.books
 WHERE schema.authors.id = schema2.books.author_id

I have never tried creating schema spanning constraints though, but in theory it should work.

1 like

Please or to participate in this conversation.