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();
相关推荐
郝学胜-神的一滴14 小时前
系统设计:新鲜事系统扩展与优化
java·python·职场和发展·php·软件工程·软件构建
JSON_L16 小时前
Fastadmin中实现敏感词管理
数据库·php·fastadmin
hhb_61816 小时前
PHP 8.x 核心特性与工程化开发实践指南
开发语言·php
网络安全许木1 天前
自学渗透测试第21天(基础命令复盘与DVWA熟悉)
开发语言·网络安全·渗透测试·php
爱学习的小囧1 天前
ESXi VMkernel 端口 MTU 最佳设置详解
运维·服务器·网络·php·虚拟化
WangJunXiang61 天前
GFS分布式文件系统
开发语言·php
Chasing__Dreams1 天前
Redis--基础知识点--29--Redis瓶颈
数据库·redis·php
ZHOUPUYU1 天前
PHP性能优化实战:提升你的应用速度
android·性能优化·php
特长腿特长2 天前
LVS的DR模式和NET模式的基础案例
服务器·php·lvs
卵男(章鱼)2 天前
汽车网络通讯分析与仿真工具的系统工程:Vector CANoe与ZLG ZCANPRO深度剖析
开发语言·汽车·php