如何获得连接行数

时间:2011-12-29 18:23:29

标签: sql-server sql-server-2008 tsql

有这个SQL语句

    SELECT t1.Name
    ,Count(t2.SubID) Totals -- I don't know how to do it.
    FROM Table t1
    INNER JOIN Table2 t2 ON t1.ID = t2.SubID 

感谢。

2 个答案:

答案 0 :(得分:3)

猜测 ...更改JOIN并添加GROUP BY

SELECT t1.Name
,Count(t2.SubID) AS Totals
FROM Table t1
LEFT OUTER JOIN Table2 t2 ON t1.ID = t2.SubID 
GROUP BY t1.Name

LEFT OUTER JOIN允许您找到每t1.Name

的计数零行

答案 1 :(得分:1)

SELECT t1.Name, Count(t2.SubID) Totals
FROM Table t1
INNER JOIN Table2 t2 ON t1.ID = t2.SubID 
GROUP BY t1.Name