我有两张桌子 说表-A和表-B
Table-A
id | Article Name |date
1 | ABC | 25/2/2011
2 | xyz | 26/2/2011
Table-B
id | Comment | Article-id
1 | good | 1
2 | nice article | 2
3 | Apreciable | 1
所需的输出是
Table-C
id | Article Name | date | NumberOfComments
1 | ABC | 25/2/2011 | 2
2 | xyz | 26/2/2011 | 1
请建议!!!
答案 0 :(得分:2)
我认为这是你追求的问题。我没有测试它以确保没有错别字。您基本上加入了Table-B
,然后将结果分组回到Table-A
的结果,但计算了多少“联接”。
SELECT [Table-A].id, [Article Name], date, COUNT(*) As NumberOfComments
FROM [Table-A]
LEFT JOIN [Table-B] On [Article-id] = [Table-A].id
GROUP BY [Table-A].id, [Article Name], date
答案 1 :(得分:2)
这样的事情应该做(未经测试,不在家):
SELECT Table-A.id, Table-A.article-name, Table-A.date, (SELECT COUNT(id) FROM Table-B WHERE Article-id=Table-A.id) AS NumberOfComments FROM Table-A;
答案 2 :(得分:0)
select Table_A.*, count(*) as 'NoC' from Table_A
inner join Table_B on Table_A.id = Table_B.idart
group by Table_A.id