版主 我是想用用用户id来分表,可是分表后上面的功能就不好实现了。比如我要查 哪些用户关注了我,分表后;我不确定要查哪个表。只有将所有分表都查一次才可以知道。 不要分表呀,直接写sql语句就可以了1.查询关注了哪些用户:select `follow_user_id` from `表` where `user_id`='你的id' 2.查询被哪些用户关注了:select `user_id` from `表` where `follow_user_id`='你的id' 3.查询和某个用户共同的关注列表。select * from `表` where `user_id` in ('你的id', '某个用户id') 4.查询是否互相关注:select * from `表` where `user_id`='你的id' and `follow_user_id`='某个用户id' `user_id` in (select `follow_user_id` from `表` where `user_id`='某个用户id')当然估计还有更优秀的写法,特别是第4条
select * from `表` where `user_id`='你的id' and `follow_user_id`='某个用户id' and `user_id` in (select `follow_user_id` from `表` where `user_id`='某个用户id') 第4条少了一个and,重新写一下
分区后的表对你而言依然是一个整表(虽然被分成了多个文件),实际查询的是那个文件由mysql决定而不是你决定如果你依然想自己分表,那么需要交换 user_id、follow_user_id 列的内容形成新表,再对两个表分表
版主 我是想用用用户id来分表,可是分表后上面的功能就不好实现了。比如我要查 哪些用户关注了我,分表后;我不确定要查哪个表。只有将所有分表都查一次才可以知道。
不要分表呀,直接写sql语句就可以了1.查询关注了哪些用户:select `follow_user_id` from `表` where `user_id`='你的id'
2.查询被哪些用户关注了:select `user_id` from `表` where `follow_user_id`='你的id'
3.查询和某个用户共同的关注列表。select * from `表` where `user_id` in ('你的id', '某个用户id')
4.查询是否互相关注:select * from `表` where `user_id`='你的id' and `follow_user_id`='某个用户id' `user_id` in (select `follow_user_id` from `表` where `user_id`='某个用户id')当然估计还有更优秀的写法,特别是第4条
select * from `表` where `user_id`='你的id' and `follow_user_id`='某个用户id' and `user_id` in (select `follow_user_id` from `表` where `user_id`='某个用户id')
第4条少了一个and,重新写一下
版主 分区是什么概念呢。吾只懂电脑分区阿 还有这句话可以详细说一下吗”那么需要交换 user_id、follow_user_id 列的内容形成新表,再对两个表分表“谢谢啦!
user_id follow_user_id
1 2
1 3
1 4
按 user_id 分表后,你整检查所有的表才能得到与 follow_user_id 相连的 user_id
所以你需要另有一组以 follow_user_id 分组的表
follow_user_id user_id
2 1
3 1
4 1
版主
"follow_user_id 分组的表" 也是要分表的吧
那这个方案可以支持查,哪些人关注了我吗。
比如我的用户id是1 按follow_user_id 尾数进行分表。
follow_user_tbl_1follow_user_id user_id
1 1
11 1
21 1follow_user_tbl_2follow_user_id user_id
2 1
12 1
22 1
那么 follow_user_id user_id 表示 follow_user_id 关注了 user_id你把主体确定了,事情就清楚了
1 1
11 1
21 1follow_user_tbl_2follow_user_id user_id
2 1
12 1
22 1楼上说的不就是一个用户关联表么?为什么是两个?一个就可以实现这个功能了。