2017-05-28 82 views
0

这是使用查询创建的视图:SELECT title,count(*)as views FROM articles a,log l WHERE a.slug = substring(l.path, 10)GROUP BY标题ORDER BY views DESC;在PostgreSQL中连接两个表和一个视图以获得所需结果

 title | views 
------------------------------------+-------- 
    article-1 | 338647 
    article-2 | 253801 
    article-5 | 170098 
    article-3 | 84906 
    article-8 | 84810 
    article-7 | 84557 
    article-6 | 84504 
    article-4 | 84383 

现在我试图用这个视图创建一个查询,打印出来的文章观点的总和与标题相关联的每个作者。下面我重新创建了另外两个表格的关键字段。

AUTHORS 
      name  | id 
---------------------+-------- 
      author-1 | 1 
      author-2 | 2 
      author-3 | 3 
      author-4 | 4 

ARTICLES 
    author  | article 
--------------+-------- 
      1 | article-1 
      2 | article-2 
      1 | article-3 
      4 | article-4 
      1 | article-5 
      1 | article-6 
      3 | article-7 
      2 | article-8 

回答

0

你会做:

select a.author, sum(v.views) as view 
from yourview v join 
    articles a 
    on v.title = a.article join 
    authors au 
    on a.author = au.id 
group by a.author; 
+0

完美的帮助,谢谢!我对数据库非常陌生,JOIN是我绝对需要处理的一件事。 –

相关问题