我有一些数据库。
我的数据库的架构 https://www.dropbox.com/s/57todvdrwnl9ooe/2222.jpg
我的数据库具有下一个结构:1。包含列的新闻表(Id,Name,Id_Heading)2。标题/子标题表(Id,Name)3。标题和子标题之间的引用表(Id_Heading,Id_Subheading)bouth Id来自标题/副标题表
例如我有标题 - “音乐”(id = 1)和副标题“Industrial”(id = 2),“Punk”(id = 3)等等。另外我有ID_of_news的表新闻, Name_of_news和ID_of_Heading。例如:1(某些名称)2。其他新闻是2(某些名称)3 ....等等。
我希望得到一张表格,其中包含两行“标题”或“标题”标题以及此标题中有多少“新闻”
我尝试使用以下查询
SELECT COUNT(n.Id) as Test, heading.Name
FROM [Heading\Subheading] heading
INNER JOIN [Relationship_Heading] tf ON tf.Id_Heading = heading.Id
LEFT JOIN [Heading\Subheading] subheading ON subheading.Id = tf.Id_Subheading
LEFT JOIN News n ON subheading.Id = n.Id_Heading
LEFT JOIN News n2 ON heading.Id = n2.Id_Heading
GROUP BY heading.Name
但是这个请求给了我每个子标题中有多少新闻的数量,但在标题
中没有答案 0 :(得分:0)
这个怎么样:
SELECT COUNT(heading.Id_Heading) as Test, heading.Id_Heading
FROM [Heading\Subheading] heading
INNER JOIN [Table References] tf ON tf.Id_Heading = heading.Id_Heading
LEFT JOIN [Heading\Subheading] subheading ON subheading.id_Heading = tf.Id_Subeading
LEFT JOIN News n ON subheading.id_Heading = n.Id_Heading
LEFT JOIN News n2 ON heading.id_Heading = n2.Id_Heading
GROUP BY heading.Id_Heading
更新:抱歉,将内部联接更改为左联接,是否正常按预期工作?