I have the situation where I have three tables:

```
hgn_modern_sources
+----+--------------+
| id |    source    |
+----+--------------+
| 17 | Something... |
+----+--------------+

junc_modern_source_has_reference
+----+------------------+------------------------+
| id | modern_source_id | location_within_source |
+----+------------------+------------------------+
| 52 |               17 | 1, 1                   |
| 89 |               17 | 1, 1                   |
| 99 |               17 | 1, 1                   |
+----+------------------+------------------------+

hgn_picture_has_reference
+----+---------------------+-------------------+
| id | modern_reference_id | picture_link      |
+----+---------------------+--------------------+
| 45 | 89                  | /images/image1.png |
| 75 | 99                  | /images/image2.png |
+----+---------------------+--------------------+

```

My question is how to delete duplicates from `junc_modern_source_has_reference` (id = 89 and id = 99), leave one row with the lowest ID (id = 52), and replace foreign keys in `hgn_picture_has_reference` table (column `modern_reference_id`) with one left (id = 52)?

The solution can be only MySQL based, or PHP + MySQL (which I prefer more).