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();
相关推荐
oMcLin23 分钟前
如何在 Ubuntu 24.04 上安装 LAMP 堆栈(包括 PHP 8.3 和 MariaDB 11)
ubuntu·php·mariadb
运维闲章印时光38 分钟前
单位本部与分部网络已实现互联互通,网络访问通畅,数据传输正常
开发语言·网络·php
福尔摩斯张17 小时前
Linux的pthread_self函数详解:多线程编程中的身份标识器(超详细)
linux·运维·服务器·网络·网络协议·tcp/ip·php
Maybe I Simple18 小时前
注解路由 + ApiDoc接入
php·html5·webman
笙枫1 天前
LangGraph Agent 架构基础:从概念到第一个可运行的Agent
开发语言·架构·php
小代码20161 天前
ubuntu vscode docker php 环境搭建
vscode·ubuntu·docker·php·laravel
Maybe I Simple1 天前
二进制打包|phar打包
php·webman
zorro_z1 天前
ThinkPHP8学习篇(十四):模板
php
JaguarJack1 天前
如何使用 PHP 的 for、while 和 foreach 循环实现极致性能与零 Bug 代码
后端·php
BingoGo1 天前
如何使用 PHP 的 for、while 和 foreach 循环实现极致性能与零 Bug 代码
后端·php