นับ Distinct values ใช้ COUNT(DISTINCT column)
Syntax
SELECT COUNT(DISTINCT yourColumnName) FROM yourTableName;
ตัวอย่าง
mysql> CREATE TABLE DemoTable (Name VARCHAR(20), Score INT);
mysql> INSERT INTO DemoTable VALUES
-> ('John',56), ('Sam',89), ('John',56), ('Carol',60), ('Sam',89), ('Carol',60);
ข้อมูล (มีซ้ำ)
+-------+-------+
| Name | Score |
+-------+-------+
| John | 56 |
| Sam | 89 |
| John | 56 |
| Carol | 60 |
| Sam | 89 |
| Carol | 60 |
+-------+-------+
Query: นับ Distinct Score
mysql> SELECT COUNT(DISTINCT Score) FROM DemoTable;
ผลลัพธ์ (3 ค่าที่ไม่ซ้ำ: 56, 89, 60)
+-----------------------+
| COUNT(DISTINCT Score) |
+-----------------------+
| 3 |
+-----------------------+