How do you join three or more records from the same table? I have table with a lot of data in it, and we would like a way to essentially flag certain documents to indicate that they are related to (x) other documents:

TABLE A              
-----------
id
name
created_at
modified_at

Records:   id        name              created_at              modified_at
            1       arlo g    2020-04-29 12:30:00      2020-04-29 12:30:00
            2     kenny ws    2020-04-29 12:32:00      2020-04-29 12:32:00
            3      roger w    2020-04-29 12:36:00      2020-04-29 12:36:00
            4     frank lw    2020-04-29 12:41:00      2020-04-29 12:41:00
            5    depeche m    2020-04-29 12:43:00      2020-04-29 12:43:00
            6       manson    2020-04-29 12:46:00      2020-04-29 12:46:00


Now - assume a user is viewing document id 1. They want to tie it to documents 3 and 4.

If a user then opens document 1, the system can easily pull documents 3 and 4. 


On the same note - if a user opens document 3, they can easily pull documents 1 and 4.


Is this possible? I am not sure how to structure a query clause to pull the document ids for a given document, such that the list is the same each time no matter which one of those documents is queried on.
If a user then opens document 1, the system can easily pull documents 3 and 4.
On the same note - if a user opens document 3, they can easily pull documents 1 and 4.

This looks like the relation have NO parent-child type. So the solution may be
CREATE TABLE r ( group_id INT UNSIGNED,
                 table_a_id INT UNSIGNED,
                 PRIMARY KEY (table_a_id)
              /* PRIMARY KEY (group_id, table_a_id) */
               );

So if you need in &quot;if a user opens document 3, they can easily pull documents 1 and 4&quot;, then all 3 table_a values (1,3,4) must belong the same group.
The primary key selection is dependent by the fact does a definite document may belong to one group only or to more than one group.
When you need to obtain the whole group members by one member of a group you will use
SELECT t2.*
FROM table_a t1
JOIN r       r1   ON t1.id = r1.table_a_id
JOIN r       r2   ON r1.group_id = r2.group_id 
JOIN table_a t2   ON r2.table_a_id = t2.id
WHERE t1.name = 'initial document name'