MYSQL选择往复数据? [英] MYSQL Selecting reciprocating data?
本文介绍了MYSQL选择往复数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为Follow
的表,其中包含三个字段:
I have a table called Follow
, with three fields:
- Id(自动增量整数)
- UserId(int),
- 关注(int)
如果我有这样的数据:
ID UserId Following
--------------------------
1 2 3
2 3 2
3 2 5
4 2 6
5 3 5
我将如何找到用户2的朋友(即:用户2正在关注他们,而他们又跟随了用户2)
How would I find user 2's friends (ie: user 2 is following them, and they follow user 2)
我想换句话说,如果用户"a"跟随用户"b",而用户"b"跟随用户"a",我该如何选择用户A?
I guess, in other words, if user 'a' follows user 'b', and user 'b' follows user 'a', how do I select user A ??
推荐答案
尝试一下:
SELECT a.UserId, a.Following
FROM Follow a INNER JOIN Follow b
ON a.UserId = b.Following
AND b.UserId = a.Following
这篇关于MYSQL选择往复数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文