小编典典

从两个表中获取数据?

sql

我有两个表说表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

请建议!


阅读 147

收藏
2021-04-15

共1个答案

小编典典

这样的事情应该做(未经测试,不在家):

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;
2021-04-15