Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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/8/mysql/55.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
Php 如何将选择限制为只选择具有公共值的行?_Php_Mysql_Arrays - Fatal编程技术网

Php 如何将选择限制为只选择具有公共值的行?

Php 如何将选择限制为只选择具有公共值的行?,php,mysql,arrays,Php,Mysql,Arrays,以下是我的表格结构: // users +----+--------+ | id | name | +----+--------+ | 1 | Jack | | 2 | Peter | | 3 | John | | 4 | Barman | | 5 | Ali | +----+--------+ // friends +---------+-----------+ | user_id | friend_id | +---------+-----------+ |

以下是我的表格结构:

// users
+----+--------+
| id |  name  |
+----+--------+
| 1  | Jack   |
| 2  | Peter  |
| 3  | John   |
| 4  | Barman |
| 5  | Ali    |
+----+--------+

// friends
+---------+-----------+
| user_id | friend_id |
+---------+-----------+
| 1       | 3         |
| 1       | 4         |
| 1       | 5         |
| 3       | 1         |
| 3       | 2         |
| 3       | 4         |
| 5       | 2         |
+---------+-----------+

-- both user_id and friend_id columns refer to the id column of users table
我的问题是:

// $id = 1;
select distinct f1.user_id, f1.friend_id
from friend f1
where user_id = $id
      or 
      user_id in (select f2.friend_id
                  from friend f2
                  where user_id = $id);
/* output
| 1       | 3         |
| 1       | 4         |
| 1       | 5         |
| 3       | 2         |
| 3       | 4         |
| 5       | 2         |
如您所见,我的查询选择

杰克因为$id=1 杰克的所有朋友 杰克朋友的所有朋友 好的,很好。实际上,我正试图绘制一张结果图。事实上我做到了。现在我想把结果限制在普通朋友身上。我的意思是我想删除单个节点。换句话说,我想选择至少有两条边的朋友

是否可以通过更改查询来实现这一点,还是应该在PHP层中实现这一点

可视示例及其预期输出:


下面的查询将返回所有非直接好友的普通好友:

select distinct f.user_id, f2.friend_id common_friend
from friends f
inner join friends f2
      on f.friend_id = f2.user_id
left join friends f3
      on f.user_id = f3.user_id
      and f2.friend_id = f3.friend_id
where f3.user_id is null
and f.user_id <> f2.friend_id
#and f.user_id = 1 Jack
第一个“内部”联接返回朋友圈,第二个“左”联接从圈->其中f3.user_id为null删除一级朋友中联接一级朋友。 最后一个f.user\u id f2.friend\u id是删除一个对他来说是普通朋友的用户。

试试这个

SELECT DISTINCT f1.user_id, f1.friend_id
FROM friend f1
WHERE (
   user_id = $id
   OR user_id IN (
      SELECT f2.friend_id
      FROM friend f2
      WHERE user_id = $id)) 
   AND (friend_id  IN (
      SELECT f3.friend_id
      FROM friend f3
      WHERE user_id = $id))

在第二个选择中添加选择计数条件。。这应该可以完成任务。更新您的问题,并将实际结果添加到您的选择和预期结果中。。根据你的数据sample@scaisEdge已添加预期输出。我不理解您的预期输出。你在找朋友的朋友,对吗?那么为什么会有1-3和1-4呢?