朋友圈数据表设计?朋友圈怎么设计数据表?
朋友圈数据表设计?朋友圈数据表怎么设计?相信大家都能看到,QQ空间里的状态是全可见的,而微信朋友圈的状态只有好友可见。同时别人的状态下面,也仅能看到自己的朋友的评论和点赞。这是如何做到的呢?程序猿小编从MySQL数据表的设计来探讨一种实现方法。
现在有这么一张好友关系图:
图中连线代表了两人互为好友(状态相互可见),当然像不让某好友看自己状态,不看某好友的状态这些功能也容易实现,实现方法后面会提到。这里以常规的状态相互可见作为主要探讨内容。
例如:高育良发表一条新状态,而高小琴、祁同伟、李达康都点了赞。在高书记的视角下应该是这样的:
表的设计
在这里共设计4张表。分别为状态表(tb_newmessage),用户表(tb_user),好友表(tb_friends)和点赞表(tb_like)。我们没有考虑评论的可见*,因为实现起来和点赞基本相同。
状态表的创建:
create table if not exists tb_newmessage(
message varchar(50) not null,
mid int not null,
uid int not null
)engine=InnoDB default charset=utf8;1234512345
用户表的创建:
create table if not exists tb_user(
uid int not null,
uname varchar(50) not null
)engine=InnoDB default charset=utf8;12341234
好友表的创建:
create table if not exists tb_friends(
firstuid int not null,
seconduid int not null
)engine=InnoDB default charset=utf8;12341234
点赞表的创建:
create table if not exists tb_like(
uid int not null,
mid int not null
)engine=InnoDB default charset=utf8;12341234
我们需要对用户表中的用户id加唯一索引:
alter table tb_user add unique(uid);11
对状态表的消息id加主键索引:
alter table tb_newmessage add primary key(mid);11
加入用户实例:
insert into tb_user(uid, uname) values(100, 'GaoYuliang');
insert into tb_user(uid, uname) values(101, 'GaoXiaoqin');
insert into tb_user(uid, uname) values(102, 'QiTongwei');
insert into tb_user(uid, uname) values(103, 'LiDakang');
insert into tb_user(uid, uname) values(104, 'HouLiangping');1234512345
添加好友关系:
insert into tb_friends values(100, 101);
insert into tb_friends values(101, 100);
insert into tb_friends values(100, 102);
insert into tb_friends values(102, 100);
insert into tb_friends values(101, 102);
insert into tb_friends values(102, 101);
insert into tb_friends values(100, 103);
insert into tb_friends values(103, 100);
insert into tb_friends values(103, 104);
insert into tb_friends values(104, 103);
insert into tb_friends values(102, 104);
insert into tb_friends values(104, 102);
insert into tb_friends values(100, 100);
insert into tb_friends values(101, 101);
insert into tb_friends values(102, 102);
insert into tb_friends values(103, 103);
insert into tb_friends values(104, 104);123456789101112131415161718123456789101112131415161718
可能已经发现,上面对好友关系的添加是A-B和B-A各加了一次,这是有实际含义的。可以分别表示A的状态对B可见,B的状态对A可见。前面提到过,这一点可以用来扩展不看某人,和不让某人看的功能。
此时,高书记发表了一个状态:
insert into tb_newmessage values('hello, very happy to be a good man!', 1000, 100);11
高小琴,祁同伟,李达康点赞:
insert into tb_like values(101, 1000);
insert into tb_like values(102, 1000);
insert into tb_like values(103, 1000);123123
效果就如同上图显示的那样了。
业务逻辑实现
in python
import MySQLdb,sys
import random
try:
conn=MySQLdb.connect(host='10.170.16.47',
port=3306,user='',passwd ='',db ='test')
conn.autocommit(1)
cur=conn.cursor()
loginuser = 100 # 登录用户的id
OpenToMeUsersSQL = """select seconduid from tb_friends where firstuid=""" + str(loginuser)
cur.execute(OpenToMeUsersSQL)
openlist = cur.fetchall() # 查询到所有我可见的用户
openlist2 = [] # 将可见用户的id收集在列表中
for orow in openlist:
openlist2.append(orow[0])
#for seconduid in openlist:
# print seconduid[0]
for op in openlist: # get all the open person id
GetMessageSQL = """select message, mid from tb_newmessage where uid="""+str(op[0])
cur.execute(GetMessageSQL)
Mlist = cur.fetchall() # 得到一个具体好友的所有状态
for ms in Mlist:
messcontent = ms[0] # 消息体
GetLikeUser = """select tb_user.uname,tb_user.uid from tb_like,tb_user where
tb_like.mid=""" + str(ms[1]) +""" and tb_like.uid=tb_user.uid"""
cur.execute(GetLikeUser)
likelist = cur.fetchall() # 得到点赞人的昵称和id
GetOpName = """select uname from tb_user where uid="""+str(op[0])
cur.execute(GetOpName)
opname = cur.fetchall()
print opname[0][0]+":"+ms[0]
for row in likelist:
if (row[1] in openlist2) : #点赞人是我的好友吗
print row[0]+" like this"
cur.close()
conn.close()
except Exception, e:
print e
sys.exit(0)
这是小编上班的时候在公司偷偷给大家总结下来的朋友圈设计表的代码,嘻嘻,希望多大家有所帮助,更多相关资讯,请持续关注微素达素材网。
相关推荐:
《怎么获取微信朋友圈数据?》
《朋友圈数据分析?》
《导出朋友圈数据,如何导出朋友圈数据?》