Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取用户和普通朋友的SQL查询_Sql_Postgresql - Fatal编程技术网

获取用户和普通朋友的SQL查询

获取用户和普通朋友的SQL查询,sql,postgresql,Sql,Postgresql,我知道以前也有人问过和回答过类似的问题,我已经复习过了,但仍然不知道如何在我的案例中做到这一点 我想创建一个我使用postgreSQL的查询,该查询将从我的数据库中返回按名称过滤的用户,按发送请求的用户与给定用户的共同好友数排序 数据结构如下: 我有一个users表,它有一个名为search\u full\u name的列,该列以ADAM SMITH的格式存储name+姓氏。这就是我过滤的东西。 我有一个user\u friends表,用于存储关于谁是谁的朋友的信息。所以我有两列:user\u

我知道以前也有人问过和回答过类似的问题,我已经复习过了,但仍然不知道如何在我的案例中做到这一点

我想创建一个我使用postgreSQL的查询,该查询将从我的数据库中返回按名称过滤的用户,按发送请求的用户与给定用户的共同好友数排序

数据结构如下:

我有一个users表,它有一个名为search\u full\u name的列,该列以ADAM SMITH的格式存储name+姓氏。这就是我过滤的东西。 我有一个user\u friends表,用于存储关于谁是谁的朋友的信息。所以我有两列:user\u id和friend\u id。数据是对称的,即每1,3有一个3,1条目。 到目前为止,在朋友搜索中,我只是使用了如下查询

select * from users where users.search_full_name like '%query%'
但是现在,我想根据用户请求的共同好友数量对结果进行额外排序,因此我的查询将有两个输入:query和userId

事实证明,我对sql的掌握不如我想象的那么好,我真的非常感谢您的帮助,如果能看到一些解释也会很好

我将期望的输出想象为:

+---------+------------------+----------------------+--+
| user_id | search_full_name | common_friends_count |  |
+---------+------------------+----------------------+--+
|      45 | Adam Smith       |                   14 |  |
|     123 | Adam Cole        |                   11 |  |
|      12 | Adamic Kapi      |                    0 |  |
+---------+------------------+----------------------+--+
对于像“Adam”这样的查询

我已经试了一整天了,我觉得我的大脑已经爆炸了。
请帮助,谢谢

基本思想是自动加入。以下内容获取与指定用户共享好友的用户的匹配项:

select uf2.user_id, count(*) as num_friends
from user_friends uf join
     user_friends uf2
     on uf2.friend_id = uf.friend_id and
        uf2.user_id <> uf2.user_id
where uf2.user_id = ?
group by uf2.user_id
order by count(*) desc;  -- the user you care about

基本思想是自连接。以下内容获取与指定用户共享好友的用户的匹配项:

select uf2.user_id, count(*) as num_friends
from user_friends uf join
     user_friends uf2
     on uf2.friend_id = uf.friend_id and
        uf2.user_id <> uf2.user_id
where uf2.user_id = ?
group by uf2.user_id
order by count(*) desc;  -- the user you care about

好的,几个小时后,我提出了一个有效的查询:这里是供将来参考的:

select u.id, u.search_full_name, count(uf.friend_id) as common_friend_count
from users u left join user_friends uf on (u.id = uf.user_id and uf.friend_id in (select friend_id from user_friends where user_id = ?))
where u.search_full_name like ?
group by u.search_full_name, u.id
order by common_friend_count desc;

好的,几个小时后,我提出了一个有效的查询:这里是供将来参考的:

select u.id, u.search_full_name, count(uf.friend_id) as common_friend_count
from users u left join user_friends uf on (u.id = uf.user_id and uf.friend_id in (select friend_id from user_friends where user_id = ?))
where u.search_full_name like ?
group by u.search_full_name, u.id
order by common_friend_count desc;

您如何知道提出问题的用户是谁?样本数据和期望的结果也会使你的问题更容易回答。理解。我将从我的应用程序中的请求上下文中获取它,并将其作为parameter@GordonLinoff,我编辑了我的帖子,包括了一个想要的结果。你怎么知道问这个问题的用户是谁?样本数据和期望的结果也会使你的问题更容易回答。理解。我将从我的应用程序中的请求上下文中获取它,并将其作为parameter@GordonLinoff,我编辑了我的帖子并包含了一个想要的结果我需要查询users表,因为我想通过查询短语搜索用户,只需按当前用户的普通朋友数量排序即可。所以我需要有一个选择从用户u。。。其中u.search_full_名称,如“%query%”。在哪里。。。是,是魔术是必要的,以某种方式加入用户的朋友表,以便能够按计数订购。请参阅原始postbtw中的所需输出。我在一个类似的问题中看到了你的帖子,使用了这种自连接方法,但在我的示例中,我无法真正让它起作用。你有什么建议吗?Thanks我需要查询users表,因为我想通过查询短语搜索用户,只需按当前用户的普通朋友数量排序即可。所以我需要有一个选择从用户u。。。其中u.search_full_名称,如“%query%”。在哪里。。。是,是魔术是必要的,以某种方式加入用户的朋友表,以便能够按计数订购。请参阅原始postbtw中的所需输出。我在一个类似的问题中看到了你的帖子,使用了这种自连接方法,但在我的示例中,我无法真正让它起作用。你有什么建议吗?谢谢