我有以下表格:
trendingtopic
id
name
trendingtopiccycle
id
trendingtopic_id(FK)
tweet
id
text
tt_cycle_id(FK)
user_id(FK)
user
id
rank
我想知道是否有一种方法可以有效地检索每个热门话题的排名较高的用户发出的推文。
这是我当前的查询:
SELECT tt."name",
MAX(tu."actual_rank") AS rank,tu."name"
FROM "trendingtopics_twitteruser" tu
LEFT JOIN "trendingtopics_tweet" tw ON tu."id" = tw."user_id"
LEFT JOIN "trendingtopics_trendingtopiccycle" ttc ON tw."tt_cycle_id" = ttc."id"
LEFT JOIN "trendingtopics_trendingtopic" tt ON ttc."tt_id" = tt."id"
GROUP BY tt."name"
它不起作用,因为根据 Postgresql:'“tu.name”必须出现在 GROUP BY 子句中或在聚合函数中使用'。
如果我将 tu."name" 添加到 GROUP BY 子句中,我最终会得到如下内容:
"106 & Park";0;"910Prince"
"106 & Park";0;"ActressAlexiss"
"106 & Park";0;"AmeliaThirlwall"
"106 & Park";0;"_ArielIvy"
"106 & Park";0;"_AyyeVce"
"106 & Park";0;"barcastuff"
"106 & Park";0.42141;"pareexo"
"106 & Park";0.0363;"khleosupporters"
"#15ThingsAboutMyCrush";0;"_ALoyalLady"
"#15ThingsAboutMyCrush";0.22275;"AmberrNikole"
"#15ThingsAboutMyCrush";0;"a_paigeturner"
"#15ThingsAboutMyCrush";0.33942018;"ArleneAndrea_xo"
但我想得到的是:
"106 & Park";0.42141;"pareexo"
"#15ThingsAboutMyCrush";0.33942018;"ArleneAndrea_xo"