Converting MySQL query to PostgreSQL. Order by day()?

59 views Asked by At

I'm converting this code from MySQL into PostgreSQL. PostgreSQL doesn't have a day() function for my Order by to work. Is there a known solution? I'm wanting to group the results by day.

        {

        SELECT to_char(date, 'YYYY-mm-dd') AS date
        FROM \"users exercises\"
        WHERE userid = $user->id AND (date >= '$begin' AND date <= '$end')
        GROUP BY day(date)
        ORDER BY date

        ) UNION ALL (

        SELECT to_char(date, 'YYYY-mm-dd') AS date
        FROM \"users foods\"
        WHERE userid = $user->id AND (date >= '$begin' AND date <= '$end')
        GROUP BY day(date)
        ORDER BY date

        }
1

There are 1 answers

1
Andomar On BEST ANSWER

MySQL's day() returns the day of the month. The PostgreSQL equivalent of that is:

GROUP BY extract(day from "date")