1

My Plex server's metadata has incorrect dates. I'm trying to set metadata_items.added_at to its associated media_parts.created_at, but you can only find the association through a 3rd table (media_items).

Here's the relevant table schemas:

| media_parts                     |
| ------------------------------- |
| id | media_item_id | created_at |

| media_items           |
| --------------------- |
| id | metadata_item_id |

| metadata_items |
| -------------- |
| id | added_at  |

I can get the information using a 3-table join:

SELECT
    media_parts.created_at AS media_created_at,
    metadata_items.added_at AS metadata_added_at
FROM media_parts
INNER JOIN media_items
ON media_parts.media_item_id = media_items.id
INNER JOIN metadata_items
ON media_items.metadata_item_id = metadata_items.id

But I'm struggling to update the metadata_items table since you can't use JOIN in an UPDATE. For example, I think I want to do the following:

UPDATE metadata_items
SET added_at = (
    SELECT
        media_parts.created_at
    FROM media_parts
    INNER JOIN media_items
    ON media_parts.media_item_id = media_items.id
    INNER JOIN metadata_items
    ON media_items.metadata_item_id = metadata_items.id
)

But it errors.

All examples I'm finding are solutions to replace a 2-table join or are setting a constant value.

How do I associate metadata_items with media_parts using media_items to update added_at with the values from created_at?

Here's a sql dump as requested to reproduce a minimal db:

CREATE TABLE media_parts(
   id            INTEGER  NOT NULL PRIMARY KEY 
  ,media_item_id INTEGER  NOT NULL
  ,created_at    VARCHAR(19) NOT NULL
);
INSERT INTO media_parts(id,media_item_id,created_at) VALUES (33,33,'2016-05-13 19:59:06');
INSERT INTO media_parts(id,media_item_id,created_at) VALUES (44,44,'2015-10-12 02:15:21');
INSERT INTO media_parts(id,media_item_id,created_at) VALUES (72,72,'2016-01-01 02:13:58');
INSERT INTO media_parts(id,media_item_id,created_at) VALUES (118,118,'2016-03-05 19:57:32');

CREATE TABLE media_items(
   id               INTEGER  NOT NULL PRIMARY KEY 
  ,metadata_item_id INTEGER  NOT NULL
);
INSERT INTO media_items(id,metadata_item_id) VALUES (33,34);
INSERT INTO media_items(id,metadata_item_id) VALUES (44,45);
INSERT INTO media_items(id,metadata_item_id) VALUES (72,73);
INSERT INTO media_items(id,metadata_item_id) VALUES (118,117);

CREATE TABLE metadata_items(
   id       INTEGER  NOT NULL PRIMARY KEY 
  ,added_at VARCHAR(19) NOT NULL
);
INSERT INTO metadata_items(id,added_at) VALUES (34,'2019-05-13 20:00:54');
INSERT INTO metadata_items(id,added_at) VALUES (45,'2018-10-12 02:19:14');
INSERT INTO metadata_items(id,added_at) VALUES (73,'2019-01-01 02:42:43');
INSERT INTO metadata_items(id,added_at) VALUES (117,'2019-03-07 20:58:50');
forpas
  • 160,666
  • 10
  • 38
  • 76
oats
  • 378
  • 2
  • 7
  • Does this answer your question? [Update table values from another table with the same user name](https://stackoverflow.com/questions/3845718/update-table-values-from-another-table-with-the-same-user-name) – Bishan Dec 12 '22 at 04:01
  • Please read the tag info wiki for the tag you used ( https://stackoverflow.com/tags/sqlite/info ) and provide a MRE as described there. – Yunnosch Dec 12 '22 at 04:14
  • @Bishan no, that only resolves it for 2 tables (table_a, table_b). Here's what I arrive to when I try to apply the accepted answer: `UPDATE metadata_items SET added_at = ( SELECT media_parts.created_at FROM media_parts, media_items WHERE media_parts.media_item_id = media_items.id AND media_items.id = metadata_items.id )` It's a syntax error, I can't reconcile metadata_items directly to media_parts, which is why the 3rd table (media_items) is required. – oats Dec 12 '22 at 05:26
  • 1
    @Yunnosch I added an MRE to the end of the question – oats Dec 12 '22 at 05:58
  • What is your version of SQLite? – forpas Dec 12 '22 at 06:00
  • @forpas version of sqlite db is 3.35.5 – oats Dec 12 '22 at 06:06

1 Answers1

1

You can use SQLite's UPDATE ... FROM syntax:

UPDATE metadata_items AS mdi 
SET added_at = mp.created_at
FROM media_items AS mi INNER JOIN media_parts AS mp
ON mp.media_item_id = mi.id
WHERE mi.metadata_item_id = mdi.id;

See the demo.

forpas
  • 160,666
  • 10
  • 38
  • 76