PostgreSQL - GROUP BY clause

后端 未结 2 1557
囚心锁ツ
囚心锁ツ 2020-12-04 02:44

I want to search by tags, and then list all articles with that tag, and also how many of given tags they match. So for example I might have:

 Page1 - 2 (has          


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 03:12

    When you use a "GROUP BY" clause, you need to enclose all columns that are not grouped in an aggregate function. Try adding title to the GROUP BY list, or selecting "min(a.title)" instead.

    SELECT COUNT(t.tag), a.title FROM a_tags t
    JOIN w_articles2tag a2t ON a2t.tag = t.id 
    JOIN w_article a ON a.id = a2t.article 
    WHERE t.tag = 'css' OR t.tag = 'php' GROUP BY t.tag, a.title LIMIT 9
    

提交回复
热议问题