Wilf
Wilf

Reputation: 2315

How to count row from table2 based on table1 include null?

I want to count rows from table2 based on table1 id. Everything works well until table2 have no related id. I want the return to be zero rather skip it.

Here's my code:

     Select *, 
      Count(pic.pic_id) As npic
       From photo_db pic
 Inner Join products_db prd 
         On pic.pic_sid=prd.prd_sid
   Group By pic.pic_sid

Because the file from photo_db is somehow not yet uploaded so no data there. Is there any possibility?

Upvotes: 1

Views: 27

Answers (1)

Fahmi
Fahmi

Reputation: 37473

Use left join instead of inner join

select pic.pic_sid, count(pic.pic_id) as npic
from photo_db pic
left join products_db prd on pic.pic_sid=prd.prd_sid
group by pic.pic_sid

Upvotes: 2

Related Questions