A few problems here. First, the presence of COUNT(*)
turns this into an aggregate query, which do you not want. That's probably the cause of your trouble.
Second, if you have a lot of rows in your appuntamento
table the performance of this query will be bad, because you can't use an index.
Presuming that you want the time and duration of the last (latest-in-time) row from a particular day in your table, and the number of appointments for that same day, you need to do this:
SELECT a.start, a.duration, b.count
FROM (
SELECT start,
duration
FROM appuntamento
WHERE start >= DATE('2014-04-08 18:30:00')
AND start < DATE('2014-04-08 18:30:00') + INTERVAL 1 DAY
ORDER BY start DESC, duration DESC
LIMIT 1
) AS a
JOIN (
SELECT COUNT(*) AS count
FROM appuntamento
WHERE start >= DATE('2014-04-08 18:30:00')
AND start < DATE('2014-04-08 18:30:00') + INTERVAL 1 DAY
) AS b
Explanation: First, this form of searching on start
allows you to use an index on the start
column. You want that for performance reasons.
WHERE start >= DATE('2014-04-08 18:30:00')
AND start < DATE('2014-04-08 18:30:00') + INTERVAL 1 DAY
Second, you need to handle the COUNT(*) as a separate subquery. I have done that.
Third, you can definitely do ORDER BY start DESC
and it will work if start
is a DATETIME column. No need for UNIX_TIMESTAMP()
.
Fourth, I used ORDER BY start DESC, duration DESC
to arrange to return the longest appointment if there happen to be several with the same start
time.