Coba ini untuk hasil yang bijaksana:
SELECT gender, COUNT(User_id) AS count
FROM User
GROUP BY gender;
Keluaran:
| gender | count |
|--------|-------|
| F | 4 |
| M | 2 |
Coba ini untuk hasil baris bijaksana dengan total keseluruhan:
SELECT (IFNull(gender,'Total')) AS gender,
COUNT(User_id) AS Count
FROM User
GROUP BY gender
WITH rollup;
Keluaran:
| gender | Count |
|--------|-------|
| F | 4 |
| M | 2 |
| Total | 6 |
Coba ini untuk hasil bijaksana kolom:
SELECT
COUNT(CASE WHEN gender = 'M' THEN User_id END) AS males,
COUNT(CASE WHEN gender = 'F' THEN User_id END) AS females,
COUNT(*) AS Total
FROM User;
Keluaran:
| males | females | Total |
|-------|---------|-------|
| 2 | 4 | 6 |