关联二个表的操作. SELECT A.ID, A.userID, A.PlanName, A.Count, B.clickCount, B.clickMoneyCount FROM dbo.Plan AS A INNER JOIN (SELECT SUM(clickCount) AS clickCount, SUM(clickMoneyCount) AS clickMoneyCount, ID FROM dbo.Click GROUP BY ID) AS B ON A.ID = B.ID
isnull
------解决方案-------------------- select a.* , isnull(b.col , 0) from a left join b on a.id = b.id
------解决方案--------------------
SQL code
SELECT
A.ID, A.userID, A.PlanName, A.Count, ISNULL(B.clickCount,0),ISNULL( B.clickMoneyCount,0)
FROM
dbo.Plan AS A
LEFT JOIN
(SELECT SUM(clickCount) AS clickCount, SUM(clickMoneyCount) AS clickMoneyCount, ID
FROM
dbo.Click GROUP BY ID) AS B ON A.ID = B.ID
------解决方案--------------------