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

phpMick's avatar
Level 15

MySQL Subquery in Query Builder

Can someone please explain how to do this in Query Builder:

select *
from (
         select *
         from a_table
     ) a
         inner join another_table at on at.id = a.id;

Obviously greatly simplified.

0 likes
2 replies
Niush's avatar

Tried this tool, and it surprisingly worked pretty well. https://sql-to-laravel-builder.herokuapp.com/

DB::table(DB::raw('(select * from a_table) a'))
  ->join(DB::raw('another_table at'),'at.id','=','a.id')
  ->get();

Probably, someone else can explain or provide better solution then this.

Please or to participate in this conversation.