在mysql中设置好友列表 [英] setting up a friend list in mysql
问题描述
我想在我的在线游戏中做朋友列表。我不知道如何设置它或从哪里开始。添加朋友和接受朋友部分我可以处理,但我不知道如何设置它在mysql和php。
如果你的友谊关系是对称的,你可以存储每对在单独的记录中:
friend1朋友2
AB
BA
AC
BD
CB
DB
并查询所有 B
的朋友:
SELECT friend2
FROM friends
WHERE friend1 ='B'
或存储最少
在第一个字段中,并且在第二个字段中具有最大的 id
:
friend1 friend2
AB
AC
BD
并查询 B
的朋友:
SELECT friend1
FROM friends
WHERE friend2 ='B'
UNION ALL
SELECT friend2
FROM friends
WHERE friend1 ='B'$ b $第一个选项是 MySQL c ,这是唯一的选择,如果你的友谊关系不是对称的(如 LiveJournal
)
:
I want to make a friends list in my online game. I am not sure how to set it up or where to start. The add friends and accept friends part I can handle, but I don't know how to set it up in mysql and php. A list of users (friends) connected to each user or something?
解决方案 If your friendship relationship is symmetrical, you can either store each pair in a separate record:
friend1 friend2
A B
B A
A C
B D
C B
D B
and query all B
's friends like that:
SELECT friend2
FROM friends
WHERE friend1 = 'B'
or store the user with the least id
in the first field and that with the greatest id
in the second one:
friend1 friend2
A B
A C
B D
and query B
's friends like that:
SELECT friend1
FROM friends
WHERE friend2 = 'B'
UNION ALL
SELECT friend2
FROM friends
WHERE friend1 = 'B'
The first option is a little bit more efficient in MySQL
, and this is the only option if your friendship relationship is not symmetrical (like on LiveJournal
)
See this article:
这篇关于在mysql中设置好友列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!