Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

436
Views
query and subquery join in laravel

i m trying this on laravel but i m getting error like this

SQLSTATE[42601]: Syntax error: 7 ERROR: subquery in FROM must have an alias
 LINE 3: (select * from subcategory a inner join s...
HINT: For example, FROM (SELECT ...) [AS] foo. (SQL: select * from 
mastercategory c
inner join 
 (select * from subcategory a inner join subling b on 
a.idsubcategory=b.idsubcategory_subcategory) on c.idcategory=a.idsubcategory_subcategory 

i m trying to join mastercatergory with subquery of join of (subcategory and subling)

   $users = DB::select(DB::raw(" select *  from mastercategory c
            inner join  (select * from subcategory a inner join subling b on 
            a.idsubcategory=b.idsubcategory_subcategory) on 
            c.idcategory=a.idsubcategory_subcategory "))->get();

how i can accomplised this i don't want to use model thanks in advance

here is my update

   $users  =   DB::raw(" select a.*,b.*  from mastercategory a
                  inner join  
               (select b.*,c.* from subcategory b inner join subling c  on  
           b.idsubcategory=c.idsubcategory_subcategory) on 
          a.idcategory=b.idcategory_mastercategory"); 

why i m getting empty value

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

 $users = DB::select("select *  from mastercategory c
            inner join  (select * from subcategory a inner join subling b on 
            a.idsubcategory=b.idsubcategory_subcategory) as d on 
            c.idcategory=d.idsubcategory_subcategory");

When you use DB::select it execute the statement and return the result. You don't need to use get and DB::raw with it (check here).

You were getting this error because you didn't provide the alias for inner subquery.

over 4 years ago · Santiago Trujillo Report

0

You have to name each subquery. For example, use this:

select * from mastercategory c
inner join 
  (select * from subcategory a 
   inner join subling b on a.idsubcategory=b.idsubcategory_subcategory) as sub 
on c.idcategory=sub.idsubcategory_subcategory

I've added the bolded part: a.idsubcategory=b.idsubcategory_subcategory) as sub

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!