我有三张桌子; articles
,likes
和shares
。我想收集50篇文章,这些文章具有最多的个人观点,喜欢和分享。这将作为基于这三个条件获取趋势文章的查询。
我将在下面描述简化的表格结构:
制品
+----+-----------+-------+
| id | title | views |
+----+-----------+-------+
| 1 | Article 1 | 92 |
| 2 | Article 2 | 14 |
| 3 | Article 3 | 39 |
| 4 | Article 4 | 87 |
| 5 | Article 5 | 8 |
+----+-----------+-------+
喜欢
+----+-----------+
| id | articleID |
+----+-----------+
| 1 | 2 |
| 2 | 3 |
| 3 | 2 |
| 4 | 5 |
| 5 | 3 |
| 6 | 3 |
+----+-----------+
股
+----+-----------+----------+
| id | articleID | type |
+----+-----------+----------+
| 1 | 1 | facebook |
| 2 | 3 | facebook |
| 3 | 1 | twitter |
| 4 | 4 | twitter |
| 5 | 2 | facebook |
+----+-----------+----------+
我不确定获取热门文章的最佳公式。每篇文章的观看量总是占主导地位,这意味着如果我将总观看次数,喜欢和份额添加到一个汇总值,结果将或多或少取决于最高的观看量:
[0] => "Article 1"
[1] => "Article 4"
[2] => "Article 3"
[3] => "Article 2"
[4] => "Article 5"
我的问题是;如何制作最佳的趋势查询"根据这三个条件?
我正在寻找的公式是基于观看次数,喜欢和分享的整体百分比值。例如,第1条具有以下百分比值:
Views: 38.33% // Article's views divided by all articles' combined views (92 / 240)
Likes: 0% // Article's likes divided by all articles' combined likes (0 / 6)
Shares: 40% // Article's likes divided by all articles' combined shares (2 / 5)
Total: 78.33 // Calculation: 38.33 + 0 + 40
第1条将有"趋势点" 78.33。对所有文章执行此操作应提供以下结果:
[0] => "Article 3" // 86.25
[1] => "Article 1" // 78.33
[2] => "Article 2" // 59.16
[3] => "Article 4" // 56.25
[4] => "Article 5" // 19.99
如何进行此类MySQL查询?
答案 0 :(得分:0)
视图始终是可预测的,因此我建议您跟踪多少百分比喜欢和分享观看次数。
答案 1 :(得分:0)
我没有得到多少优化,但是要获得完全趋势输出,这就是我要做的:
create table articles (id integer, title varchar, views integer);
create table likes (id integer, articleID integer);
create table shares(id integer, articleID integer, type varchar);
insert into articles(id,title,views)values(1,'Article 1',92);
insert into articles(id,title,views)values(2,'Article 2',14);
insert into articles(id,title,views)values(3,'Article 3',39);
insert into articles(id,title,views)values(4,'Article 4',87);
insert into articles(id,title,views)values(5,'Article 5',8);
insert into likes(id,articleID) values(1,2);
insert into likes(id,articleID) values(2,3);
insert into likes(id,articleID) values(3,2);
insert into likes(id,articleID) values(4,5);
insert into likes(id,articleID) values(5,3);
insert into likes(id,articleID) values(6,3);
insert into shares(id, articleID, type)values(1,1,'facebook');
insert into shares(id, articleID, type)values(2,3,'facebook');
insert into shares(id, articleID, type)values(3,1,'twitter');
insert into shares(id, articleID, type)values(4,4,'twitter');
insert into shares(id, articleID, type)values(5,2,'facebook');
select A.id, A.title,COALESCE(
A.views+(select count(id) from likes where id=A.id group by id)
+(select count(*) from shares where articleid = A.id
group by articleid),0) as Freq
from articles A order by Freq desc;
id | title | freq
----+-----------+------
1 | Article 1 | 95
4 | Article 4 | 89
3 | Article 3 | 41
2 | Article 2 | 16
5 | Article 5 | 0
(5 rows)
答案 2 :(得分:0)
我可能已经使用下面的查询来解决这个问题:
SELECT articles.id,
articles.title,
(
coalesce(articles.views / A.totalViews, 0) +
coalesce((SELECT count(*) FROM likes WHERE articleID = articles.id) / L.totalLikes, 0) +
coalesce((SELECT count(*) FROM shares WHERE articleID = articles.id) / S.totalShares, 0)
) * 100 as trendingPoints
FROM articles
CROSS JOIN (SELECT sum(views) as totalViews FROM articles) A
CROSS JOIN (SELECT count(*) as totalLikes FROM likes) L
CROSS JOIN (SELECT count(*) as totalShares FROM shares) S
GROUP BY articles.id
ORDER BY trendingPoints DESC
这是最佳还是可以更有效地编写?