- SQL code
SELECT A.[pkID], A.[OrgName], COUNT(B.[pkID]) AS JoinCount, COUNT(C.[pkID]) AS InfoCountFROM [dbo].[WebSite_UMS_List_Org] ALEFT JOIN [dbo].[WebSite_CMS_Info_Model_Work_Dictionary_Org] BON A.[pkID] = B.[OrgID] AND B.[flagStatus] = 0LEFT JOIN [dbo].[WebSite_CMS_Info_Relation_Org] CON A.[pkID] = C.[OrgID] AND C.[flagStatus] = 0WHERE A.[flagStatus] = 0GROUP BY A.[pkID], A.[OrgName], A.[SortID]ORDER BY A.[SortID]
A表:
- SQL code
pkID ParentID SortID OrgName OrgNameAS flagStatus27 3 0 市统计局 市统计局 028 3 0 市农委 市农委 029 3 0 市人口计生委 市人口计生委 0B表:[code=SQL]pkID OrgID flagStatus1 28 0
C表:
- SQL code
pkID InfoID OrgID flagStatus1 234 28 02 123 28 03 456 28 0
查询结果,想要显示的是:
- SQL code
pkID OrgName JoinCount InfoCount27 市统计局 0 028 市农委 1 329 市人口计生委 0 0
结果显示的是:
- SQL code
pkID OrgName JoinCount InfoCount27 市统计局 0 028 市农委 3 329 市人口计生委 0 0
就是JoinCount里,1和3的区别。
------解决方案--------------------
COUNT(DISTINCT B.[pkID]) AS JoinCount