# Sqlite3 query to get recent TV episodes

**URL:** <https://forums.plex.tv/t/sqlite3-query-to-get-recent-tv-episodes/368927>\
**Category:** General Discussions\
**Created:** [January 29, 2019, 5:31pm UTC](https://forums.plex.tv/t/sqlite3-query-to-get-recent-tv-episodes/368927 "2019-01-29T17:31:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lbutlr](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/lbutlr/32/3944_2.png) [@lbutlr](https://forums.plex.tv/u/lbutlr)\
**Post date:** [January 29, 2019, 5:31pm UTC](https://forums.plex.tv/t/sqlite3-query-to-get-recent-tv-episodes/368927/1 "2019-01-29T17:31:04Z")

</div>

I have a script that I run that returns the 5 most recent movies in my Plex server, and I have tried to do the same thing to give me a list of the most recent TV episodes, but am unable to get ti to work.

Basically, I want an sqlite3 command that I can executer at the shell that returns something like “Modern Family, The magicians, You’re the worst, The Daily Show, The Goldbergs” when I ask for the 5 newest episodes. I don’t want the episode titles just the series name, but I cannot figure out how to get it.

I thought something like this

```
SELECT * FROM metadata_items WHERE library_section_id=1 and title like "%You Owe Me a Unicorn%";

```

would at least get me started, but that doesn’t show a series name at all (It’s The Passage).

If I do this instead

```
SELECT * FROM metadata_items join media_items WHERE metadata_items.library_section_id=1 and title like "%You Owe Me a Unicorn%";

```

I do get results that list "The%20Passage, but I get results that include every single episode of every single series with the info for that episode prepended to each line.

---

<div class="post-metadata">

**Author:** ![elan](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/elan/32/5_2.png) [@elan](https://forums.plex.tv/u/elan)\
**Post date:** [January 29, 2019, 8:51pm UTC](https://forums.plex.tv/t/sqlite3-query-to-get-recent-tv-episodes/368927/2 "2019-01-29T20:51:28Z")

</div>

Try this (might need to change section ID):

```auto
select episodes.id,shows.title,episodes.title,seasons.`index`,episodes.`index` from metadata_items as episodes join metadata_items as seasons on seasons.id=episodes.parent_id join metadata_items as shows on shows.id=seasons.parent_id where episodes.library_section_id=2 and episodes.metadata_type=4 order by episodes.added_at desc limit 10;

```

---

<div class="post-metadata">

**Author:** ![lbutlr](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/lbutlr/32/3944_2.png) [@lbutlr](https://forums.plex.tv/u/lbutlr)\
**Post date:** [January 29, 2019, 11:23pm UTC](https://forums.plex.tv/t/sqlite3-query-to-get-recent-tv-episodes/368927/3 "2019-01-29T23:23:33Z")

</div>

> [@elan](#):
>
> select episodes.id,shows.title,episodes.title,seasons.`index`,episodes.`index` from metadata\_items as episodes join metadata\_items as seasons on seasons.id=episodes.parent\_id join metadata\_items as shows on shows.id=seasons.parent\_id where episodes.library\_section\_id=2 and episodes.metadata\_type=4 order by episodes.added\_at desc limit 10;

Wow. Well yes, that works (mostly, it lists the Daily show twice because there were two new episodes) but how would I have gotten there? Can you explain what the `index` does and what metadata\_type=4 does?

---

<div class="post-metadata">

**Author:** ![elan](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/elan/32/5_2.png) [@elan](https://forums.plex.tv/u/elan)\
**Post date:** [January 29, 2019, 11:58pm UTC](https://forums.plex.tv/t/sqlite3-query-to-get-recent-tv-episodes/368927/4 "2019-01-29T23:58:01Z")

</div>

> Can you explain what the `index` does

Index is the field which holds season and episode numbers.

> and what metadata\_type=4 does?

4 = episode  
3 = season  
2 = show  
1 = movie

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [April 29, 2019, 11:58pm UTC](https://forums.plex.tv/t/sqlite3-query-to-get-recent-tv-episodes/368927/5 "2019-04-29T23:58:05Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
