美文网首页
怎么设计好友关系库表

怎么设计好友关系库表

作者: 全栈coder | 来源:发表于2016-10-25 20:06 被阅读486次

暂时想到的是设计两张表

user 表(用户表)

id   user
1    张三
2    李四
3    王五
4    赵六

friend 表(会员关系表)

id   uid   fid
 1    1     2
 2    1     3
 3    2     1
 4    4     2
 5    4     3

如果要查找好友

 select * from friend where uid='张三' or fid='张三'

查找好友数最多的前三个人

select * from user a,friend b where a.id = b.uid
   and a.id  in(
                select uid count(*) AS cn
                FROM friend GROUP BY
                uid ORDER BY cn      
                desc LIMIT 3 
 ) 

好像不对,请高手指教。。。

相关文章

网友评论

      本文标题:怎么设计好友关系库表

      本文链接:https://www.haomeiwen.com/subject/kxaruttx.html