SELECT id,stuid FROM Stu GROUP BY stuid
SELECT MAX(id),stuid FROM stuGROUP BY stuid
如果有多条数据 ,按第一条sql 执行 GROUP by 数据,拿到的可能会是这个学生的旧数据
而第二条加上max,则可以拿到他最新的数据
每个部门工资最高的员工
select A.*
from tab_salary A inner join
(select max(salary) as salary,departid as departid from tab_depart
group by departid) B
on A.salary=B.salary and A.departid=B.departid
sql 查出某部门男女分别最高薪资
1,版本
select f.*
from money f
INNER JOIN
(select max(moneys) AS moneys,sex,`name` as departid from money
group by sex) B
on f.moneys=B.moneys and f.sex=B.sex
WHERE f.dept=1
改进:
select f.*
from money f
INNER JOIN
(select max(mn.moneys) AS moneys,mn.sex from money mn
group by mn.sex) B
on f.moneys=B.moneys and f.sex=B.sex
WHERE f.dept=1
调试sql
EXPLAIN select