我需要创建一个查询,根据用户喜欢的内容以及喜欢他们喜欢的内容的其他人向用户推荐艺术家。希望你明白。
无论如何,有问题的表是成员(自我解释)和artist_follows(成员跟随谁)。我稍后会将此查询添加到更大的查询中,这也将从艺术家表中获取该ID的艺术家信息。
这是我到目前为止所使用的,我使用2
作为成员的模拟ID。
SELECT
M.ID AS M_ID,
F.ID AS F_ID,
F.follows_ID
FROM members M
LEFT JOIN artist_follows F
ON F.ID = M.ID
WHERE M.ID ='2'
我意识到它并不是很多,但据我所知,为什么我来到这里。
关于需要做什么的细分:
1:从会员表中获取ID。
2:使用成员ID
从artist_follows获取follow_ID(用户关注的人)3:获取关注该成员的人员的IDS。 (为了下一点的目的,将其称为“其他成员IDS”)。
4:从“其他会员IDS”中获取其他艺术家IDS,确保未选择该会员已经关注的艺术家。
希望像这一样打破它有帮助!
更新<!/强>
让我们有一些模拟数据,如下所示。让我们说成员2
跟随IDS 3
的艺术家。另一位成员(4
)跟随3
以及20
。因此,查询的结果应为20
架构:
artist_follows;
CREATE TABLE `artist_follows` (
`ID` int(11) NOT NULL,
`follows_ID` int(11) NOT NULL,
KEY `fav_ID` (`ID`),
CONSTRAINT `fav_ID` FOREIGN KEY (`ID`) REFERENCES `artists` (`ID`),
CONSTRAINT `ID` FOREIGN KEY (`ID`) REFERENCES `members` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
成员;
CREATE TABLE `members` (
`ID` int(111) NOT NULL AUTO_INCREMENT,
`email` varchar(100) COLLATE latin1_general_ci NOT NULL,
`password` varchar(100) COLLATE latin1_general_ci NOT NULL,
`FNAME` varchar(100) COLLATE latin1_general_ci NOT NULL,
`SURNAME` varchar(100) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
答案 0 :(得分:1)
SELECT DISTINCT new.follows_ID
FROM artist_follows fan
INNER JOIN artist_follows other ON other.follows_ID = fan.follows_ID
INNER JOIN artist_follows new ON new.ID = other.ID
WHERE new.follows_ID NOT IN
(SELECT follows_ID FROM artist_follows WHERE ID = fan.ID )
AND fan.ID = 2;
答案 1 :(得分:1)
create table `members` (
`id` int(111) not null auto_increment,
primary key (`id`)
);
create table `artist_follows` (
`id` int(11) not null,
`follows_id` int(11) not null,
constraint `id` foreign key (`id`) references `members` (`id`)
);
insert into members (id) values (2), (4);
insert into artist_follows (id, follows_id) values
(2, 3),
(4, 3),
(4, 20);
select distinct f3.follows_id follows
from
members m
inner join
artist_follows f1 on f1.id = m.id
inner join
artist_follows f2 on f1.follows_id = f2.follows_id
inner join
artist_follows f3 on f2.id = f3.id
where
m.id = '2'
and
f3.follows_id not in (
select follows_id
from artist_follows
where id = f1.id
)