sql 分组 在线等
表A
书id
1001
1002
1003
表B
书id 评分
1001 8
1001 10
1001 7
1003 5
1003 9
获取表A按平均分排序的记录
sql
------解决方案--------------------select A.* from A
Inner join
(
select ID,AVG(评分) as avgfenshu from B
group by ID
) as c on A.ID=c.ID
Order by c.avgfenshu desc
------解决方案--------------------我也来一个
select A.id,c.平均分 from A left outer join
(
select ID,AVG(评分) as 平均分 from B
group by ID
) as c on A.ID=c.ID
Order by c.平均分 desc