2013-05-15 214 views
2

我從下面的查詢MySQL的SELECT COUNT DISTINCT

SELECT co.mobility_theme, COUNT(DISTINCT ca.user_id) AS n 
FROM courses_apply AS ca LEFT JOIN dy41s_courses AS co ON ca.course_id = co.id 
WHERE ca.submission IS NULL AND ca.`call` LIKE '1b' 
GROUP BY co.mobility_theme 
ORDER BY co.mobility_theme ASC 

得到如下結果總(133)

mobility_theme n 

1 =>70 

4 =>18 

5 =>45 

我想要做的是:爲每個移動主題(1,2和5),我有重複的用戶,所以總唯一的user_id應該是(130),我無法通過(移動性主題)獲得唯一的user_id組,任何幫助?

+0

沒關係,但是你喜歡的子句是什麼? – Drew

+0

它只是獲得指定的「電話」 – MMSA

+0

的記錄我知道,但它不是'like' – Drew

回答

3

一種方法是任意選擇一種移動性主題。這種方法選擇最小值的那個:

select mobility_theme, COUNT(*) as Numusers 
from (select ca.user_id, MIN(co.mobility_theme) as mobility_theme 
     from courses_apply ca LEFT JOIN 
      dy41s_courses co 
      ON ca.course_id = co.id 
     WHERE ca.submission IS NULL AND ca.`call` LIKE '1b' 
     group by ca.user_id 
    ) caco 
group by mobility_theme 
+0

你讓我的一天:)))))))))))))) – MMSA