拥有此数据库架构(仅用于说明目的)
class a{
public:
string words[3];
a::a() {
words[0] = "cake";
words[1] = "pie";
words[2] = "steak";
}
};
使用MySQL可以做到:
[articles (id_article, title)]
[articles_tags (id_tag, id_article)]
[tags (id_tag, name)]
导致您在每行文章的标题和文章的标记计数上产生的结果,例如
SELECT a.title, COUNT(at.id_tag) tag_count FROM articles a
JOIN articles_tags at ON a.id_article = at.id_article
JOIN tags t ON t.id_tag = at.id_tag
GROUP BY a.id_article
ORDER BY tag_count DESC
由于ORACLE不支持SELECT语句中的非聚合列,因此无论如何都可以在一个查询中执行此操作吗?当您通过向SELECT语句添加聚合函数或将列添加到GROUP BY语句来满足ORACLE的需求时,您已经获得了不同的结果。
提前致谢
答案 0 :(得分:1)
是的,这是可能的。在SELECT列表中返回id_article
,而不是title
,并将整个查询包装在parens中以使其成为内联视图,然后从中进行选择,并加入articles
表获取关联的title
。
例如:
SELECT b.title
, c.tag_count
FROM ( SELECT a.id_article
, COUNT(at.id_tag) tag_count
FROM articles a
JOIN articles_tags at ON a.id_article = at.id_article
JOIN tags t ON t.id_tag = at.id_tag
GROUP BY a.id_article
) c
JOIN articles b
ON b.id_article = c.id_article
ORDER BY c.tag_count DESC
您还可以评估是否确实需要内联视图中包含的articles
表。我们可以改为GROUP BY at.id_article
。
我认为这会返回一个等效的结果:
SELECT b.title
, c.tag_count
FROM ( SELECT at.id_article
, COUNT(at.id_tag) tag_count
FROM articles_tags at
JOIN tags t ON t.id_tag = at.id_tag
GROUP BY at.id_article
) c
JOIN articles b
ON b.id_article = c.id_article
ORDER BY c.tag_count DESC