//先统计用户的消费金额
$buildsql= Db::name('account_log a')
->field('sum(a.amount)')
->where('a.user_id=u.user_id')
->where('a.type',17)
->buildSql(); //此语法没有数据库操作
//然后查询
$userList = Db::name('users u')
->field("u.*,$buildsql as sm")
->where($condition)
->order($sort_order)
->limit($Page->firstRow . ',' . $Page->listRows)
->select();
//打印sql
echo DB::name('users ')->getlastsql();exit;
//打印出来是
SELECT
`u`.*, (
SELECT
sum(a.amount)
FROM
li_account_log a
WHERE
(a.user_id = u.user_id)
AND `a`.`type` = 17
) AS sm
FROM
li_users u
ORDER BY
user_id DESC
LIMIT 0,
10;
查询用户表里的用户并统计每个用户的消费金额
最新推荐文章于 2023-03-11 14:37:41 发布