0

I'm trying to group data by month for the current year. The query below works on MySQL database but gives an error when connected to PostgreSQL.

 $results = DB::table('visits')
        ->select(DB::raw('month(visitdate) month, count(*) data'))
        ->where('team_id', 2)
        ->whereYear('visitdate', now()->year)
        ->groupBy('month')
        ->get();

This error was generated:

SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "month" LINE 1: select month(visitdate) month, count() data from "visits" w... ^ (SQL: select month(visitdate) month, count() data from "visits" where "team_id" = 2 and extract(year from "visitdate") = 2021 group by "month")

I tried a similar query with Eloquent and got the same results.

The visitdate column stores the date in the format "2021-01-01" I'm using laravel 8 with Breeze

1 Answer 1

0

month is a keyword. You should choose something else instead, or put it in double quotes, or in this case just put AS in front of it. Any of the three should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.