Select from a comma separated field

Let's say I have a subscribers table that has a row for each user like this.

 id name subscribers 1 user1 user2,user3,user4 2 user2 user4,user5,user3 3 user3 user1,user6,user2 etc... 

What I want to do is run a select statement like this.

  SELECT subscribers from table where id = '1' 

.. And then, limit the number of subscribers to show me ie if I limited it to 2, it would only mean SELECT "user2,user3" from table.subscribers WHERE id=1

I know that I can limit it after selecting everything with PHP, but I do not want to run into performance issues if there are millions of usernames in each column ...

It is also the best framework for setting up a subscibe/follow . Or is there a better way?

+4
source share
2 answers

You save several values ​​in one field. This is bad!

You will need a second table to represent the subscription - it will have a userid and subscriberuserid column (or something similar).

For each subscriber that the user has, in this table there will be an entry with this user userid (and user ID).

Then you can limit the content of your hearts:

 SELECT subscribers.subscriberuserid FROM subscribers WHERE userid = 1 LIMIT 2 
+7
source

I think a Many-to-Many relationship would be more convenient, you could easily limit your record numbers ...

0
source

Source: https://habr.com/ru/post/1333165/


All Articles