1. 程式人生 > >mysql變數使用(leetCode中一分組排序題)

mysql變數使用(leetCode中一分組排序題)

select      d.name as Department,     e. Employee,     e.Salary from (     select      temp.name as Employee,      temp.Salary,      temp.DepartmentId,     (case when @p_DepartmentId = temp.DepartmentId then @p_count := @p_count +1 else @p_count := 1 end ) rownum ,     (@p_DepartmentId := temp.DepartmentId)     from Employee temp ,      ( select @p_DepartmentId := null ,@p_count := 1 ) as a      ORDER BY DepartmentId,Salary desc )e ,Department d  where d.id = e.DepartmentId  and rownum<4 ORDER BY DepartmentId,rownum