MySQL - 过滤最后一项

时间:2015-02-19 15:29:40

标签: mysql

我正在尝试计算各种课程中涉及的不同专业。我已经完成了它,但是注意到如果他们注册了多个课程,我计算重复的用户名。你能帮我在这个查询的某个地方找到DISTINCT标签吗?

SELECT a.`major_desc` AS `major`, 
(SELECT COUNT(a.`major_desc`) FROM all_students WHERE major_desc=`major` LIMIT 1) AS `count` 
FROM all_students AS a 
INNER JOIN all_course_reg AS b ON a.username=b.username 
INNER JOIN courses AS c ON b.`crn`=c.`crn` 
GROUP BY `major`

编辑:sqlfiddle - > http://sqlfiddle.com/#!2/81bb0/3/0

2 个答案:

答案 0 :(得分:0)

在我看来,这就是你所追求的 - 我不知道其他所有东西是什么......

SELECT major_desc, COUNT(*) cnt  
  FROM all_students
 GROUP
    BY major_desc;

答案 1 :(得分:0)

我们走了。感谢我的男孩3mHz。

SELECT a.major_desc, 
   (select count(a.username)) as test
FROM all_students AS a 
WHERE EXISTS (select * from all_course_reg
    inner join courses on courses.crn=all_course_reg.crn
    where all_course_reg.username=a.username)
GROUP BY a.major_desc

因此,如果在课程集合中提供课程,我们现在可以获得每个专业的正确计数。