Skip to content
Advertisement

How to select from two selected tables in Laravel Query Builder?

I want to convert my SQL to Laravel Query Builder version, however I can’t do that. Can anyone help me to solve this case or give me some advices, thank you.

Original SQL that work fine in mysql server.

Converted the original SQL to Laravel Query Builder, but it doesn’t work.

Error: SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘(select COUNT(job_master.appno)’ doesn’t exist

Advertisement

Answer

DB::table doesn’t really work with subqueries.

But you can use fromSub and joinSub. These will take closures or query builder queries to be able to define your subqueries.

You can also access the query builder using DB::query() since you can’t use a specific table.

You’ll find each line of sql could be represented by a function in the query builder. It is worth looking through the documentation, or even the source code just to find all the functions.

So I came up with something like this:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement