更新时间:2023-12-04 13:48:48
此处的解决方案实际上是您的数据库结构稍有更改.我建议您创建一个多对多" 关系表,其中包含用户引用的所有用户朋友.
The solution here is actually a slight change in your database structure. I recommend you create a "many-to-many" relational table containing all of the users friends referenced by user.
+---------+-----------+
| user_id | firend_id |
+---------+-----------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 1 |
| 2 | 5 |
+---------+-----------+
如果要在一个字段中存储值列表,则这是数据库设计不是十分理想的第一个迹象.如果您需要搜索一个数值,***在该字段上放置一个索引以提高效率并使数据库为您服务,而不是反过来:)
If you are storing lists of values within one field then that is the first sign that your database design is not quite optimal. If you need to search for a numerical value, it'll always be better to place an index on that field to increase efficiency and make the database work for you and not the other way around :)
然后要找出用户是否是某人的朋友,您将查询此表-
Then to find out if a user is a friend of someone, you'll query this table -
SELECT * FROM users_friends WHERE
`user_id` = CURRENT_USER AND `friend_id` = OTHER_USER
要获得某个用户的所有朋友,您可以这样做-
To get all the friends of a certain user you would do this -
SELECT * FROM users_friends WHERE `user_id` = CURRENT_USER