mysql 使用变量

$subQuery= Db('end_user_log')->alias('i2')->field("count(id) as end_user_log_count")->where('i2.user_id=i.id and i2.type=2')->limit(1)->buildSql();
$list =EndUserModel::alias('i')
->field('i.*,@counts:=('.$subQuery.') as end_user_log_count , (remaining_usage_count + @counts) as alls')
->where($where)
->where(['agent_id'=>$this->agentMsg['id']])
->order($enorder)
->paginate(10);

子查询出的结果集和当前语句的结果端求和

i为用户表,i2为记录表,用户表中remaining_usage_count是剩余数量,
i2 统计使用次数(end_user_log_count ), 两个加起来为总次数

SELECT @query1:=(subquery) AS s1,@query2:=(subquery) AS s2,(@query1+@query2) AS s3
点赞

发表评论