mysql - 关于php查询数据库的问题
Jun 06, 2016 pm 08:29 PM
我正在做一个学校内部交流系统,现在遇到一个问题
比如:用户A的ID是5,用户B的ID是6,如何实现用户A关注的人同样也关注B,怎么统计出来人数呢?
已知的表:Follow_table(关注表)
<code>follow_id(自增ID) | user_id(关注者ID) | follow_user_id(被关注者ID) | follow_time(关注时间) | -------------------------------------------------------------------------------------------------- 1 | 5 | 1 | 2015-08-22 10:29:11 | </code>
以上是表的结构。
请问mysql查询语句怎么写呢?感谢大家!!
回复内容:
我正在做一个学校内部交流系统,现在遇到一个问题
比如:用户A的ID是5,用户B的ID是6,如何实现用户A关注的人同样也关注B,怎么统计出来人数呢?
已知的表:Follow_table(关注表)
<code>follow_id(自增ID) | user_id(关注者ID) | follow_user_id(被关注者ID) | follow_time(关注时间) | -------------------------------------------------------------------------------------------------- 1 | 5 | 1 | 2015-08-22 10:29:11 | </code>
以上是表的结构。
请问mysql查询语句怎么写呢?感谢大家!!
查A关注的人里有多少关注B的,是这意思吧
<code>select count (1) from Follow_table where follow_user_id=6 and user_id in (select follow_user_id from Follow_table where user_id=5)</code>
是说A、B同时关注的人,建议分别取A关注的人和B关注的人然后取数组交集这样会降低数据库负载,因为数据库CPU资源更宝贵。
一条sql的话试试这样:
<code>select a.follow_user_id from Follow_table a, Follow_table b where a.user_id=5 and b.user_id=6 and a.follow_user_id=b.follow_user_id;</code>
先查询到用户a关注的人,然后再判断这些人有没有关注b
确定不用redis???
假设a->b,a->[b,c,d,e],会不会有c->b或d->b呢,设定->左侧为left(关注人),右侧为right(被关注人),先找到a所关注的对象[right],再以右侧b找到所有左侧关注人[left],在从[left]中找[right],SQL语句就不写了。。。

Heißer Artikel

Hot-Tools-Tags

Heißer Artikel

Heiße Artikel -Tags

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian

So beheben Sie den Fehler „mysql_native_password nicht geladen' unter MySQL 8.4
