0

我有一个 schedule_item 表,我可以在其中安排要显示的动画。

播放动画后,它的属性 is_played 设置为 true。

当所有动画都播放完后,调度器将之前显示的动画添加到队列中,所以屏幕总是播放一些动画。

当新动画到达时,它需要在队列之前添加 - 理想情况下替换下一个要播放的项目。

schedule_item 如下所示:

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'
);

有些行可能看起来像这样

+----+--------------+-----------+
| id | animation_id | is_played |
+----+--------------+-----------+                                           
| 17 |           15 |         1 |
| 40 |           22 |         1 |
| 43 |           26 |         1 |
| 46 |           15 |         1 |
| 49 |           22 |         0 |
+----+--------------+-----------+

所以,我试图获取所有尚未播放但过去播放过的动画的 id。在这种情况下,我正在寻找 id 49 / animation_id 22。

所以这就是我想出的,它似乎正在工作,但是:

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;
  • 有更好的方法吗?
  • 我得到正确的结果了吗?
4

1 回答 1

1

试试这个查询——在这种情况下你不需要一个having子句。

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
    )
于 2012-06-28T11:35:15.437 回答