Support the ongoing development of Laravel.io →
posted 10 years ago
Database

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: 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)

As I check the error sql statement it run exactly what I want in sql client. But it does not work in Laravel

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

shiroamada shiroamada Joined 13 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.