我有一个用户和一个朋友表。具有用户详细信息的用户和朋友具有用户的朋友。这是用户表的架构..
create table smsusers(
id varchar(60),
password varchar(50) not null,
fname varchar(30) not null,
lname varchar(30),
mailid varchar(50) not null,
gender varchar(10) not null,
primary key(id)
);
和朋友桌..
create table friends_list(
friend_of varchar(60) NOT NULL,
friends_id varchar(60) NOT NULL,
friendship_status varchar(30),
friendship_date timestamp NOT NULL,
primary key(friend_of,friends_id),
foreign key(friend_of) references smsusers(id)
ON DELETE CASCADE ON UPDATE CASCADE);
用户照片
create table profile_pic(pic_id varchar(200),profile_pic_path varchar(1000),small_pic_path varchar(1000),
adddate varchar(100),userid varchar(60),foreign key(userid) references smsusers(id) ON DELETE CASCADE ON UPDATE CASCADE,
primary key(pic_id));
在这里,我想随机获取 5 个具有共同好友数的好友好友
我试过这个查询
SELECT DISTINCT ff.friends_id,
concat(u.fname,' ',u.lname),
pp.small_pic_path,
count(*) AS mutual_friend_count
FROM
friends_list f
JOIN friends_list ff
ON ff.friend_of =f.friends_id
LEFT JOIN smsusers u
ON
u.id=ff.friends_id
LEFT JOIN profile_pic pp
ON
ff.friends_id=pp.userid
WHERE f.friend_of = 1
AND ff.friends_id NOT IN (1)
GROUP BY friends_id
此查询显示朋友的朋友和我的朋友,并且相互计数也不正确。我试过的查询。