稍微复杂的MySQL查询

时间:2009-06-18 03:16:41

标签: mysql

员工     e_id最后一个     1约翰史密斯     2鲍勃史密斯     3 Alex Smith     4 John Doe     5 Ron Doe

clockpunch
e_id    time    for adjustment
1   0650    in  early
3   0710    in  late
4   0725    in  early
1   1100    lunch   ---
2   1150    in  late
2   1400    lunch   ---
4   1320    out ---

我需要一个单一的查询,列出所有员工姓名,以及该用户早期的次数,按早期次数降序排列。怎么会这样做?

2 个答案:

答案 0 :(得分:0)

这可能有效(未经测试)

select first,last,count(*) from employees e, clockpunch c 
where e.e_id = c.e_id and adjustment = 'early' 
group by first,last
order by count(*) desc

答案 1 :(得分:0)

    select a.first,a.last,sum(case b.adjustment when 'early' then 1 else 0 end) as ct 
from employees a, clockpunch b where a.e_id=b.e_id
    group by a.first, a.last