开发者

Selecting from a comma separated field

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,u开发者_如何学Goser3
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 how many subscribers to show me i.e. If I limited it to 2, it would only SELECT "user2,user3" from table.subscribers WHERE id=1

I know I can limit it after selecting all with PHP but I don't want to run into performance problems, if there were millions of usernames in each column...

Also, is this the best structure to set up a subscibe/follow system.. Or is there a better way?


You are storing multiple values in the same field. This is bad!

You need a second table, to represent subscriptions - it would have a column userid and subscriberuserid (or something similar).

For every subscriber that a user has, there would be a record in this table with that user's userid (and the userid of their subscriber).

Then, you can limit to your hearts content:

SELECT subscribers.subscriberuserid
FROM subscribers
WHERE userid = 1
LIMIT 2


I think Many-To-Many relation would be more convenient, you could then easily limit your record numbers...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜