这是一个纯 sql 问题,sql 大致这样: select u.userName, t1.a1Num, t2.a2Num from user as u, (select count(*) as a1Num from a1 where userId = ?) as t1, (select count(*) as a2Num from a2 where userId = ?) as t2 where u.id = ? 代码:Db.find(sql, userId, userId, userId);
如果要一条 sql 统计所有用户,将 userId=? 换成 userId=u.id,并且将最后面的 where u.id=? 去掉即可: select u.userName, t1.a1Num, t2.a2Num from user as u, (select count(*) as a1Num from a1 where userId = u.id) as t1, (select count(*) as a2Num from a2 where userId = u.id) as t2
select u.userName, t1.a1Num, t2.a2Num from user as u,
(select count(*) as a1Num from a1 where userId = ?) as t1,
(select count(*) as a2Num from a2 where userId = ?) as t2 where u.id = ?
代码:Db.find(sql, userId, userId, userId);