这是我的SQL语句(足够实现相关查询):
select *
from A a
join B b on a.id = b.userid
and b.booknums in (select max(c.booknum ) from B c where c.userid =a.id group by c.userid)
但是在接口出参时需要对应AB表字段,将B的booknums的最大值插入到A表的booknum中从而进行排序,问题是A,B表中的关联一对多。
也就是A表之于B表是一对多
B表查询的最大值之于A表是一对一。
一对多用开窗比较好
select max(booknum)over(partition by userid) maxnum
;with t as (select *,row_number over(partition by b.userid order by b.booknum desc) as rn
from A a
join B b on a.id = b.userid)
select * from t where rn=1