Level 24
Remove the comma after COUNT(*).
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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'
Remove the comma after COUNT(*).
Please or to participate in this conversation.