SQL选择2个表但不省略空白结果

时间:2017-07-16 17:31:30

标签: sql mysqli

我必须从3个trables中做出选择。

table1- idc,title,description..

table2- idc,filename,filepath,tabel1FK

table3- idc,table1FK,tabel2FK

我需要从table1和table2中选择并计算table3

中table1的唯一出现次数

选择必须是这样的

TB1 | TB2 | COUNT ON TB3

  a |  aa  | 1
  b |      | 4 
  c |  cc  | 3 
  d |      | 0 
  e |      | 3      

1 个答案:

答案 0 :(得分:0)

我认为您只想要left joingroup by

select tb1.idc, tb2.idc, count(tb3.idc)
from tb1 left join
     tb2
     on tb2.table1FK = tb1.idc left join
     tb3
     on tb3.table1FK = tb1.idc and tb3.tabel2FK = tb2.idc
group by tb1.idc, tb2.idc;