Home >Backend Development >PHP Tutorial >如何在一张朋友表里面查询和自己有相同朋友并且不是自己的朋友的用户

如何在一张朋友表里面查询和自己有相同朋友并且不是自己的朋友的用户

WBOY
WBOYOriginal
2016-06-13 10:16:20946browse

怎么在一张朋友表里面查询和自己有相同朋友并且不是自己的朋友的用户


uid和朋友ID都是在另外一张表里面
怎么在一张朋友表里面查询和自己有相同朋友并且不是自己的朋友的用户

------解决方案--------------------
朋友表friend中至少有两个字段,user_id字段和friend_id字段
1.首先查出自己的朋友:select friend_id from friend where user_id = "zhangshan";
2.select distinct user_id from friend where friend_id in (select friend_id from friend where user_id = "zhangshan") and user_id != "zhangshan" and user_id not in (select friend_id from friend where user_id = "zhangshan");

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn