S
S
spartax2020-08-03 13:18:22
MySQL
spartax, 2020-08-03 13:18:22

SQL query to select records from a playlist from one table depending on the id in another column?

There is a playlist table:
id | name
and table of playlist songs:
id | playlist_id | prev_id | next_id

On the front, it will be possible to move songs around the playlist (sort manually), in general, as in VKontakte.
It is planned to send the record ID - id, the ID of the previous record - prev_id and the ID of the next record - next_id to the backend.

The backend will put prev_id and next_id in the database of the moved record.
And the previous record (which has id=prev_id) will change next_id to id.
And the next entry (which has id=next_id) will change prev_id to id.

Now the question is: how to write a SQL query to select a list of playlist songs so that they are selected based on next_id?

Tell me please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
serginhold, 2020-08-03
@serginhold

it all looks like storage of trees, so google storage of trees, next_id is definitely redundant,
but why complicate it so much if you could make one field with sort storage, and queries would be simple

D
Dmitry Shitskov, 2020-08-03
@Zarom

The best way would be to store the sort order in the database, using some order column of float type. Thus, when "drag and drop", it will always be possible to conveniently shove an entry between the other two.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question