I give rank
to each row by its create_time in table instances group by (task_id, task_time) as primary key.I My SQL is as following
SELECT
task_id,
task_time,
create_time,
@rn := CASE WHEN @prev_task_id <> task_id THEN 1
WHEN @prev_task_time <> task_time THEN 1
ELSE @rn + 1 END AS rank,
@prev_task_id := task_id,
@prev_task_time := task_time
FROM instances, (SELECT
@rn := 0,
@prev_task_id := -1,
@prev_task_time := '-1') t
where task_id in (1209, 1211)
having rank = 1
ORDER BY task_id, task_time DESC, create_time DESC;
Question comes After I add having rank = 1
clause. The returning result is not accurate compared with the result without having rank = 1
and is missing some records whose rank is also 1, for example, most records with task_id = 1211
are missing.
As I know , having
clause is launched after select
for add filter to the final records. I want to know the mistakes I've made, THX.