我需要找到还书平均失踪天数最高的学生。我有 2 张桌子:
- 用户 -> id、fname、lname、专业
- 借出 -> isbn(书号), id, since(date), due(date), actual(date)
这是我这样做的方法,带有子查询
select concat(fname, '', lname) as name
from users
where id in ( select id
from loaned
group by id
having avg(datediff(due, actual))
= ( select min(m)
from ( select avg(datediff(due, actual)) as m
from loaned
group by id
) as minavg
)
);
我怎么能用 JOIN 解决这个问题?