vendredi 11 décembre 2015

SQLite query with two tables to find the "next episode to watch"

So I have two tables, one is for TV Shows and the other for Episodes, schematically:

show {
  long id;
  String title;
}

episode {
 int season;
 int episode;
 long showId (foreign key)
 int watched: Default (0)
}

I am trying to run a query that will return for each show, the next episode the user has to watch. Let's say I have two shows, Show A and Show B, each line is a season and each number an episode. I am representing the episodes with 0 (not watched) and 1 (watched).

A (2 seasons of 4 episodes)
0000
0000

B (3 seasons of 3 episodes)
000
000
000

For the previous example, the query should return:

A, season = 1, episode = 1
B, season = 1, episode = 1

More examples:

A
010
000
B
111
000

Here the query should return:

A, season = 1, episode = 3
B, season = 2, episode = 1

Another:

A
0100100101
0001000000
B
1110000001
0000000010

Here the query should return:

A, season = 2, episode = 5
B, season = 2, episode = 10

As you can see, user can skip episodes, or watch them without order but I always want to show as "next to watch" the one that is immediately next to the furthest episode watched.

Is it possible to do this in one query? (can have subqueries, of course). But I am looking for one query here so then I can attach it to an Android Loader.

Aucun commentaire:

Enregistrer un commentaire