deepu07
163
2
Laravel

Laravel query Builder

Posted 9 months ago by deepu07

Hey folks, I'm stuck-up with query builder. I have one raw sql query it is working when you execute in sequel Pro. but not working in code. can anyone help me on this? Thanks in advance.


SELECT a.*
FROM table1 a
JOIN (SELECT name, age, COUNT(*)
FROM table2 
GROUP BY name, age
HAVING count(*) > 1 ) b
ON a.name = b.name
AND a.age = b.age
ORDER BY a.name

//Query Builder

        $data = \DB::table("table1 AS a")
          ->select("a.*")
          ->join(\DB::raw("(SELECT 
              name, age, COUNT(*),
              FROM table2
              GROUP BY b.name, b.age
              HAVING count(*) > 1
              ) as b"), function($join){
                $join->on("a.name", "=", "b.name")->on("a.age", "=", "b.age");
          })
          ->orderBy("a.name")
          ->get();

        dd($data);      

Error                                                                                                                                                                             
  [PDOException]                                                                                                                                                            
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the rig  
  ht syntax to use near 'FROM table2                                                                                                                
              GROUP BY b.name, b.age'                                                                                                                                                                                                                                                                                          




  

Please sign in or create an account to participate in this conversation.