I recently took over some work form another developer and am trying to understand why a specific sql query does not work for certain timestamps, more specifically timestamps on the same date, the sql query is:
select FROM_UNIXTIME(order_time,'%Y.%m.%D') as order_time,
order_time as timealias
from orders
where order.id ='.$user->id.'
group by FROM_UNIXTIME(order_time,'%Y.%m.%D')
order by timealias desc
The idea is that the orders are grouped by date for the user to view, if the dates are on different days.
This works fine:
03/08/2012, 15:10:30 (Unix Timestamp: 1344006630)
04/08/2012, 12:10:30 (Unix Timestamp: 1344082230)
However, if they are on the same day but at different times, then it displays the first order in the table but the second.
03/08/2012, 15:10:30 (Unix Timestamp: 1344006630)
03/08/2012, 17:30:25 (Unix Timestamp: 1344015025)
My theory is that it does not recognise the second entry’s date as a match to the first entry’s, but since that date is used already it breaks, or something to that effect. But considering DBs aren’t my strongpoint, it’s a guess.
If anyone knows a proper reason why it behaves like this, I would really appreciate some insight.
Your query contains a GROUP BY on FROM_UNIXTIME. This is going to pick one “random” row for each FROM_UNIXTIME, which isn’t what you want.
If you want to see every timestamp, including duplicates:
If you want to show the number of order for each FROM_UNIXTIME: