|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
hi all, i am trouble with this query requirement.
Suppose i have the two tables: administrator: +--------+-------------+--------+ | emp_id | name | gender | +--------+-------------+--------+ | 20001 | Kate Tse | F | | 20002 | John Cheung | M | | 20003 | Kevin Cheng | M | | 20004 | Richard | M | +--------+-------------+--------+ adm_work_hours: +--------+------------+-------+ | emp_id | day | hours | +--------+------------+-------+ | 20001 | 2004-01-10 | 2.50 | | 20001 | 2004-01-11 | 2.00 | | 20002 | 2004-01-10 | 3.00 | | 20002 | 2004-01-11 | 3.50 | | 20003 | 2004-01-10 | 8.00 | +--------+------------+-------+ i need to write a query that find the total working hours of every administrators. Here is my written query: SELECT R.emp_id, R.name, sum(hours) as Total_Hours FROM Administrator as R, Adm_work_hours as S WHERE R.emp_id = S.emp_id GROUP BY R.emp_id, R.name ORDER BY Total_Hours ; However, I cannot get the work hours of Richard (emp_id=20004) as 0. How can I also show that his working hours is zero in the result? thanks |
|
#2
|
|||
|
|||
|
Query that might work. I'm pretty sure it's the LEFT JOIN....
SELECT R.emp_id, R.name, sum(hours) as Total_Hours FROM Administrator as R LEFT JOIN Adm_work_hours as S on ( R.emp_id = S.emp_id ) GROUP BY R.emp_id, R.name ORDER BY Total_Hours |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > How to do this query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|