0

我有一个table3columns看起来像

branchid  semesterid  courseid
 4          25           2        
 4          36           5
 4          23           3
 4          12           10 
 4          34           15
 4          2            7
 4          23           42

 7          23           9
 7          10           6
 7          34           3
 7          20           17

我需要那个分支的termid和courseid的计数

SELECT branchid, count(semesterid), count(courseid) WHERE branchid = 4

当我执行此查询时,count(semesterid) 显示错误计数,而 courseid 显示正确计数 7

4

3 回答 3

1

用我的水晶球,我预测你需要这个:

SELECT COUNT(DISTINCT semesterid), 
       COUNT(DISTINCT courseid) 
  FROM Table1 
 WHERE branchid = 4;

...因为显示的查询应该可以很好地计算记录数(但不是唯一记录)。

于 2012-09-13T13:41:47.533 回答
0

尝试:

SELECT branchid, 
       count(distinct semesterid) as semesterid, 
       count(distinct courseid) as courseid
FROM   <table>
WHERE  branchid = 4
group by branchid
于 2012-09-13T13:41:49.257 回答
0

如果您想要唯一结果的数量,则必须将count(id)替换为count(distinct id)

于 2012-09-13T13:43:46.223 回答