sql:共同好友qq

sql,易懂系列:

初始化:

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `qq_friends`
-- ----------------------------
DROP TABLE IF EXISTS `qq_friends`;
CREATE TABLE `qq_friends` (
  `selfId` int(11) DEFAULT NULL,
  `friendId` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of qq_friends
-- ----------------------------
INSERT INTO `qq_friends` VALUES ('1', '3');
INSERT INTO `qq_friends` VALUES ('3', '1');
INSERT INTO `qq_friends` VALUES ('1', '12');
INSERT INTO `qq_friends` VALUES ('1', '4543');
INSERT INTO `qq_friends` VALUES ('3', '12');
INSERT INTO `qq_friends` VALUES ('1', '569');
INSERT INTO `qq_friends` VALUES ('3', '569');

selfId:自己(自己的q号)

friendId:朋友(朋友的q号)

查询与某好友的共同 好友

sql:共同好友qq_第1张图片

 

1号的好友列表:

SELECT friendId FROM qq_friends WHERE selfId=1

 

1号与3号的共同好友:

SELECT friendId FROM qq_friends WHERE selfId=1
AND friendId IN(
SELECT friendId FROM qq_friends WHERE selfId=3
)

  1号与3号的共同好友个数:

SELECT COUNT(1) FROM qq_friends WHERE selfId=1
AND friendId IN(
SELECT friendId FROM qq_friends WHERE selfId=3
)

 

查询好友的好友,排除自己,排除好友,即,潜在好友。

SELECT friendId FROM qq_friends WHERE 1=1 AND selfId IN (
SELECT friendId FROM qq_friends WHERE selfId=1
) 
AND friendId!=1
AND friendId NOT IN(
SELECT friendId FROM qq_friends WHERE selfId=1
)

 

查询好友列表中没有自己的,即你被对方删除的好友,即把你删除的好友,单向好友

查询好友是不是将你删除了。罗列:

你的好友是不是删除了你,你的塑料好友

哪些好友删除了你

SELECT a.friendId FROM qq_friends a
LEFT JOIN qq_friends b ON b.selfId=a.friendId
WHERE a.selfId=1
AND b.selfId IS null

 

SELECT friendId FROM qq_friends WHERE selfId=1


SELECT COUNT(1) FROM qq_friends WHERE selfId=3 AND friendId=1

 

查询好友是不是将你删除了

SELECT COUNT(DISTINCT b.selfId) FROM qq_friends a
LEFT JOIN qq_friends b ON b.selfId=a.friendId
WHERE a.selfId=1
AND b.selfId IS NOT null
AND b.selfId=3 

正常好友:互相存在对方列表,你的正常好友。

SELECT   b.selfId FROM qq_friends a
LEFT JOIN qq_friends b ON b.selfId=a.friendId
WHERE a.selfId=1
AND b.selfId IS NOT null
GROUP BY b.selfId

 

你可能感兴趣的:(sql:共同好友qq)