考虑下表:
[Table: talks]
talkID | title | starred
-------+--------------+--------
1 | talk1-title | 1
2 | talk2-title | 1
3 | talk3-title | 0
4 | talk4-title | 0
5 | talk5-title | 0
[Table: talkspeaker]
talkID | speaker
-------+---------
1 | Speaker1
1 | Speaker2
2 | Speaker3
3 | Speaker4
3 | Speaker5
4 | Speaker6
5 | Speaker7
5 | Speaker8
[Table: similartalks]
talkID | similarTo
-------+----------
1 | 3
1 | 4
2 | 3
2 | 4
2 | 5
3 | 2
4 | 5
5 | 3
5 | 4
我想要做的是:给定已加星标的演讲集,我想选择未加星标的演讲中的前 2 个(已加星标 = 0)及其与加星标演讲集最相似的标题和演讲者。问题是获得演讲者需要使用聚合函数,获得最相似的演讲也是如此。
在没有发言者的情况下,我可以使用以下查询获得最相似的谈话:
select t2.talkID, t2.title, count(*) as count
from similarTalks s, talks t1, talks t2
where s.talkID = t1.talkID
and t1.Starred = 1
and s.similarTo = t2.TalkID
and t2.Starred = 0
group by t2.title, t2.talkID
order by count desc
limit 2
通常,我使用以下聚合函数来获取扬声器,并按列进行适当的分组(假设 t =talkpeaker):
group_concat(t.speaker, ', ') as Speakers
如在
select t1.title, group_concat(t2.speaker, ', ') as Speakers
from talks t1, talkspeaker t2
where t1.talkID = t2.talkID
group by t1.title
但我无法将这两件事结合在一起。我计划在 sqlite 数据库中运行这个查询可能很重要(这就是 group_concat 函数的来源)。与已加星标的对话最相似的前 2 个未加星标的对话的答案似乎与 talkID 3 和 4 相关。