laravel distinct查询问题,laravel子查询写法

  1. 直接调用后,count查询会和实际查询的数据对不上,count还是查询全部数据,而实际的列表是去重的。
  1. 给distinct加上参数,比如去重的值的id,就加id。

另一种写法是使用group by id + 子查询。

sql语句:

sql 复制代码
select count(id) from (
	select xx from tableName
) s

laravel子查询写法:

php 复制代码
$model1 = new XXX();
$model1 = $model1->select()->groupBy('id');
$count = $model2->from(DB::raw("({$model1->toSql()}) t")->count();
相关推荐
ServBay3 小时前
告别面条代码,PSL 5.0 重构 PHP 性能与安全天花板
后端·php
多厘1 天前
别再手写 psr-4 了!用 Composer 隐藏魔法干掉上千行烂配置
laravel
JaguarJack2 天前
FrankenPHP 原生支持 Windows 了
后端·php·服务端
BingoGo2 天前
FrankenPHP 原生支持 Windows 了
后端·php
JaguarJack3 天前
PHP 的异步编程 该怎么选择
后端·php·服务端
BingoGo3 天前
PHP 的异步编程 该怎么选择
后端·php
JaguarJack4 天前
为什么 PHP 闭包要加 static?
后端·php·服务端
ServBay5 天前
垃圾堆里编码?真的不要怪 PHP 不行
后端·php
用户962377954485 天前
CTF 伪协议
php
BingoGo7 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php