Fetch data from two tables?

前端 未结 3 1705
一整个雨季
一整个雨季 2021-01-28 10:02

I have two tables Say Table-A and Table-B

Table-A

id | Article Name |date
1  | ABC          | 25/2/2011
2  | xyz          | 26/2/2011

Table-B

id | Comment             


        
相关标签:
3条回答
  • 2021-01-28 10:51
    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
    
    0 讨论(0)
  • 2021-01-28 11:01

    Something like this should do (untested, not at home):

    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;
    
    0 讨论(0)
  • 2021-01-28 11:01

    I think this is the query you are after. I didn't test it to be sure there are no typos. You are basically joining on Table-B, and then grouping the results back down to just those from Table-A, but counting how many "joins" were made.

    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
    
    0 讨论(0)
提交回复
热议问题