thinkphp多表联合查询计算总和sum
2022-07-28 14:14:58
160
{{single.collect_count}}

原标题:TP5 数据查询一对多计算总和SUM

ccd2e2e5a48f7487825c636270669ab2.png

TP5查询语句

(1)查询语句

Db::table('student')

->alias('st')

->join(['fraction' => 'ft'], 'st.studentID=ft.studentID', 'left')

->field('st.studentID,st.studentName,ifnull(sum(ft.fraction),0) as allFraction')

->group('st.studentID')

->select()

(2)生成的sql语句

SELECT

st.studentID,

st.studentName,

ifnull( sum( ft.fraction ), 0 ) AS allFraction

FROM

`student` `st`

LEFT JOIN `fraction` `ft` ON `st`.`studentID` = `ft`.`studentID`

GROUP BY

`st`.`studentID`

执行结果

05c00790369a79d1ff3b616267ef5ee5.png

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}