MySQL Group Results by day using timestamp

Are you just forgetting to add GROUP BY ... at the end?

SELECT
    COUNT(id) as order_count,
    SUM(price + shipping_price) as order_sum,
    DAY(FROM_UNIXTIME(created)) as order_day
FROM `order`
WHERE '.implode(' AND ', $where).'
GROUP BY order_day

NOTE:

You cannot use as day for your day column because day is a MySQL function. Use something like order_day.

Of Unicorns

Per @OMG Unicorn’s comment, you can use:

DAY(FROM_UNIXTIME(created)) as `day`

So long as wrap day in ` backticks.

Leave a Comment

tech