0

I use 2 databases in my Laravel project. One is MySQL and the other is SQL Server. My main database is MySQL.
I can use these databases individually, but if I want to join and use one of them in a query, I will face a problem.

This is my code

 $list=DB::connection('mysql')->select('
 SELECT
                pp.mahsool_cod as planed_code,
                SUM(pp.meghdar) as plan_quantity,
                COUNT(*) as planed_count,
                (SELECT SUM(tolid_reports.real_meghdar)
                    FROM tolid_reports WHERE tolid_reports.plan_tolid_date BETWEEN N\''.$head->date2.'\' and N\''.$head->date4.'\'
                    and tolid_reports.plan_cod= planed_code ) as tolid_quantity,
                    (N\''.DB::connection('sqlsrv')->select('select p.Name from farsi.LGS3.Part as p where p.Code =planed_code ').'\') as namee
                FROM pr_planings AS pp
                where pp.tolid_date BETWEEN N\''.$head->date2.'\' and N\''.$head->date4.'\' GROUP by pp.mahsool_cod
  ');

I can't to use the second database in the other database subquery

Thom A
  • 88,727
  • 11
  • 45
  • 75
  • 1
    No, you cannot. The two databases do not know about each other and have no connection to each other, so a query trying to use both databases will not work. – aynber Jan 10 '23 at 13:53
  • You would need to pool the data into one shared database system. – user3783243 Jan 10 '23 at 13:53
  • 1
    If you need the data to be relational between the 2 databases why are you using 2 databased? Worse, why 2 different instances, and *even worse* 2 different products? – Thom A Jan 10 '23 at 13:54
  • Laravel does not currently support cross-database joins or subqueries – codenathan Jan 10 '23 at 14:07
  • "Laravel does not currently support" - I'd go even further and say that PHP and MySQL/MSSQL don't support this, as far as I know. Even across two servers of the same type can be [complicated](https://stackoverflow.com/a/2048085/231316). – Chris Haas Jan 10 '23 at 14:32
  • @Larnu The SQL database is for another software that I have access to. And I use its information to create a report in my software. There are foreign keys in two software – ali abbasi Jan 11 '23 at 08:55

0 Answers0