thinkphp6 查询条件为空时,部分sql语句不执行问题

最近写model时,需要做一个统计,在查询出分页列表的同时,查询所有数据统计值;例子:

$list = self::where($where)->order($sort,$order)->page(PAGE, LIMIT)->select()->toArray();
$count=self::where($where)->field("sum(money) as money, sum(tax_money) as tax_money,sum(number) as number,
        count(id) as count")->find();

如果$where是空数组,那么$count是查不到数据的,但是$list能有值;我用

var_dump(self::getLastSql());

打印sql,只能打印$list 的sql,$count应该就没执行;但是这样写是没问题的:

$list = self::where($where)->order($sort,$order)->page(PAGE, LIMIT)->select()->toArray();
 $count = self::where($where)->count();

$count是能查询到值的,我搜索了一下,thinkphp客服回答是这样的:6.0就是这么设计的 没有查询条件 是不会实际进行查询的。

也就是说需要查询条件才能正常查询,那么给啥通用的查询条件?可以这么写:

$list = self::where($where)->order($sort,$order)->page(PAGE, LIMIT)->select()->toArray();
        $count=self::where($where)->field("sum(money) as money, sum(tax_money) as tax_money,sum(number) as number,
        count(id) as count")->order("id desc")->find();

$count 加上一个order就能查询到数据了。

举报
评论 0