1

在此查询中,我需要删除重复的导师 ID 并计算它们..

SELECT COUNT(t.tutor_id) FROM tutors AS t
    INNER JOIN tutor_category_subject as tcs ON t.tutor_id = tcs.tutor_id   
    INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
WHERE s.subjects LIKE '%business%' 

这是查询输出:

+-------------------+
| COUNT(t.tutor_id) |
+-------------------+
|                 6 |
+-------------------+

但应该是 4 .. 为什么导师 id 是 3, 6, 15, 15, 16, 16

4

2 回答 2

2

利用distinct

SELECT COUNT(DISTINCT t.tutor_id) FROM tutors AS t
    INNER JOIN tutor_category_subject as tcs ON t.tutor_id = tcs.tutor_id   
    INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
WHERE s.subjects LIKE '%business%' 
于 2012-11-01T03:11:48.237 回答
0

例如,将一个名为“total”的别名放在包含计数的列中,然后在查询末尾添加一个 GROUP BY 总计。

于 2012-11-01T03:14:40.873 回答