Thursday, 12 September 2013

bring all categories of data using where

bring all categories of data using where

I have two related tables, data(no, name, citycode, age) and city(code, city)
Table city
+------+------------+
| code | city |
+------+------------+
| A1 | Jakarta |
| A2 | Bali |
| A3 | Semarang |
| A4 | Surabaya |
| C1 | Dili |
| C2 | Jayapura |
| C3 | Yogyakarta |
| C4 | Bandung |
+------+------------+
Table Data
+----+--------+----------+------+
| no | name | citycode | age |
+----+--------+----------+------+
| 1 | Ony | A3 | 27 |
| 2 | Abri | A3 | 28 |
| 3 | Denny | C4 | 27 |
| 4 | Febri | C1 | 27 |
| 5 | Galih | C3 | 28 |
| 6 | Yulia | A2 | 26 |
| 7 | Zening | A1 | 25 |
+----+--------+----------+------+
I want to count the number of employees who are age 27 by city
my query :
select city.city , count(data.name) as Nmb_of_employees
from city
left join
data on data.citycode = city.code
where data.age = 27
group by city.city;
The result
+----------+------------------+
| city | Nmb_of_employees |
+----------+------------------+
| Bandung | 1 |
| Dili | 1 |
| Semarang | 1 |
+----------+------------------+
but the result I want is like this
+------------+------------------+
| city | Nmb_of_employees |
+------------+------------------+
| Jakarta | 0 |
| Bali | 0 |
| Semarang | 1 |
| Surabaya | 0 |
| Dili | 1 |
| Jayapura | 0 |
| Yogyakarta | 0 |
| Bandung | 1 |
+------------+------------------+
what query should I use for the results as above ?

No comments:

Post a Comment