How to find the related items from different tables?

Tell me how to find the related items from different tables if one table does not have a connection.
There are tables:
1) wall - id, message, attachments
2) attachments - id, path

the table is the wall in the attachments field contains the id of the attachment and the problem is that there may be several pieces (for example: 1,324,5342). Please tell me how can I map values, or what changes to make to the table structure, to realize the idea more correctly?
April 3rd 20 at 18:51
1 answer
April 3rd 20 at 18:53
FIND_IN_SET(`attachments`.`id`, `wall`.`attachments`) > 0

But the mind is necessary to bring the base into a normal form, separate the table relationships.
Thanks for the reply. It dawned on me, when you download the file attachments will be indicated by the parent_id = 0, and at the time the record was created, after you know the record id to update/bind the data (attachments) to publikaciji. Do you think this is a normal variant or are there better? - Judge commented on April 3rd 20 at 18:56
If you re-used attachments, the the problem says to use a third table as you advised above.
3) wall_attachments - id wall_id, atach_id.
If there is no need to re-use attachments, it is sufficient to attribute foreign key in tabila investments.
attachments - id wall_id, path
PS: the name of the parent_id is used generally to refer to records in the same table, is to describe tree structures is suitable. - Eldred commented on April 3rd 20 at 18:59
@Eldred, I apologize in advance, I don't know much mysql, so I don't know what's in it for me. Each attachment is unique and can only be used in one publication. I do not understand how do I then combine all into one single structure like this:
- id
- message
(! Given that each record a different number of inputs (0 to 6)

(example of result of course, is the api method wall.get (broke my head already) - Judge commented on April 3rd 20 at 19:02
Structure you this will be:
1) wall - id, message
2) attachments - id wall_id, path
where wall_id refers to the table entry wall.
The problem is that if you want to save attachments first, you do not know how to insert wall_id.
There are plenty of options how to solve it.
Option 1.
When you create a message, the client immediately sends the message and attachments.
There are problems with the insert not. Maintain the entry wall, wall_id get it, use it to insert attachments.
Option 2.
We can't create a "fair" record wall immediately, without sending in advance the recording of attachments.
Solutions option 2 first.
Of saved attachments with omitted parameter wall_id, for example wall_id equate to null.
When you add the entry wall, known wall_id entered in the record of attachments, where it was null.
But note, if your system has multiple users, you need somehow to distinguish which user or the user's session is disconnected litter records attachments while editing posts.
To do this in the attachments table should be used an additional attribute that points to user_id or session session_id, like this:
attachments - id wall_id, user_id, path
or this:
attachments - id wall_id, session_id, path
And binding you need to take just wall_id is not null records, and specify that you need to take recordings for a specific user or session.
If the user left the edit message without saving, and he left some incoherent entries in your system, you need to provide a script that will periodically clean up a disjointed record.
To determine how long added entries to remove the outdated table attachments you need to provide the date and time of creation of the attachment. So you can easily write a query that will clean up these old records, considering their time of creation.
Solution option 2 the second.
Create a temporary entry wall, once the user requests a new message form.
The same problems of tracking temporarily created record wall and solutions are the same as in the case of incoherent investments. - Eldred commented on April 3rd 20 at 19:05
@Eldred, thank you very much, will do so. I'm downloading media files first, because this api for mobile applications is a must. - Judge commented on April 3rd 20 at 19:08

Find more questions by tags MySQL