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

6
  • 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. Commented Jan 10, 2023 at 13:53
  • You would need to pool the data into one shared database system. Commented Jan 10, 2023 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? Commented Jan 10, 2023 at 13:54
  • Laravel does not currently support cross-database joins or subqueries Commented Jan 10, 2023 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. Commented Jan 10, 2023 at 14:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.