select t.* from tb1 as tleft join(select id,count(id) as n from tb2 group by id) as a1on t.id=a1.idorder by a1.n
hql 的时候要把select * 去掉