Reputation: 383
I have a scheduled_item table where I can schedule animations to be displayed.
Once the animation is played it's attribute is_played is set to true.
When all animations have been played, the scheduler adds previously displayed animation to the queue, so the screen always plays some animations.
When the new animation arrives it needs to be added ahead of the queue - ideally replace the very next item to be played.
The schedule_item looks like this:
CREATE TABLE scheduled_item (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`animation_id` bigint(20) DEFAULT NULL,
`is_played` tinyint(1) NOT NULL DEFAULT '0'
);
Some rows could look like this
+----+--------------+-----------+
| id | animation_id | is_played |
+----+--------------+-----------+
| 17 | 15 | 1 |
| 40 | 22 | 1 |
| 43 | 26 | 1 |
| 46 | 15 | 1 |
| 49 | 22 | 0 |
+----+--------------+-----------+
So, I'm trying to get id of the all animations that haven't been played yet, but have been played in the past. In this case I'm looking for id 49 / animation_id 22.
So this is what I've come up with and it seems to be working but:
SELECT item_id
FROM scheduled_item
GROUP BY animation_id
HAVING count( animation_id ) > 1
AND MIN( is_played ) < 1
AND MAX( is_played ) > 0
ORDER BY id ASC;
Upvotes: 0
Views: 1337
Reputation: 2523
Try this query - you don't need a having
clause in this case.
select item_id from scheduled_item
where is_played = 0 and
item_id in(
select p.item_id from scheduled_item p
where p.animation_id = animation_id and
p.item_id < item_id and is_played = 1
)
Upvotes: 1