mysql计数查询

时间:2012-11-01 03:10:49

标签: mysql sql

在此查询中,我需要删除重复导师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

2 个答案:

答案 0 :(得分: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%' 

答案 1 :(得分:0)

例如,将名为“total”的别名放入带有计数的列,然后在查询结尾处添加GROUP BY总计。