I am joining two rows from the same table which are related, basically a "root row" and the latest updated row. I am hoping there is a way to set the value of a root column row to a row in the latest update's. Only in the result that is returned, I don't want to update the database row of the latest column.
/* Table Name: posts */
+--------+---------+-------------+--------------+
| ID | ROOT | PUBLISHED | DELETED |
+--------+---------+-------------+--------------+
| 01 | 01 | FALSE | NULL |
+--------+---------+-------------+--------------+
| 02 | 01 | NULL | 01/03/2019 |
+--------+---------+-------------+--------------+
| 03 | 01 | NULL | 01/04/2019 |
+--------+---------+-------------+--------------+
| 04 | 01 | NULL | NULL |
+--------+---------+-------------+--------------+
The query I'm trying to do selects row 04 because it's the latest, and I'm trying to join the root row, (01) and then make the value of row 04 have "FALSE" in the return result, while remaining "NULL" in the database.
SELECT p1.*, p2.ID, p2.PUBLISHED
FROM posts p1
LEFT JOIN posts p2 ON p1.ID = p2.ID
WHERE p1.DELETED = NULL
I've tried a few things and have been looking around but I have just been getting stuck.
SELECT p1.*, p2.ID, p2.PUBLISHED, p1.PUBLISHED = p2.PUBLISHED
FROM posts p1
LEFT JOIN posts p2 ON p1.ID = p2.ID
WHERE p1.DELETED = NULL
SELECT p1.*, p2.ID, p2.PUBLISHED, p1.PUBLISHED as p2.PUBLISHED
FROM posts p1
LEFT JOIN posts p2 ON p1.ID = p2.ID
WHERE p1.DELETED = NULL
Thank you anyone who reads this