I am doing the below SQL, perfectly work in sql query client
select `member_status`.`name`, COUNT(members.id) AS totalNum from `members` right join `status` on `members`.`status` = `member_status`.`id` and `members`.`created_at` between '2014-07-01 00:00:00' AND '2014-07-31 23:59:59' group by `member_status`.`name`
This is the Query Builder:
$query = DB::table('members');
$query->rightJoin('member_status', function ($join) use ($start_date, $end_date)
{
$join->on('members.status', '=', 'member_status.id')->where('members.created_at', 'between', '\''. $start_date . '\' AND \'' . $end_date . '\'');
});
$records = $query->select(DB::raw("`member_status`.`name`, COUNT(members.id) AS totalNum"))
->groupBy('member_status.name')
->get();
When I run the script. Laravel throw me SQL Error,
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 right syntax to use near 'group by
status
.name
' at line 1 (SQL: selectmember_status
.name
, COUNT(members.id) AS totalNum frommembers
right joinstatus
onmembers
.status
=member_status
.id
andmembers
.created_at
between '2014-07-01 00:00:00' AND '2014-07-31 23:59:59' group bymember_status
.name
)
As I check the error sql statement it run exactly what I want in sql client. But it does not work in Laravel
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community