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 小时前
K8S 单 Master 集群在 openEuler 24.03 上的部署指南
容器·kubernetes·php
AI-小柒16 小时前
磅上线!DataEyes 聚合平台正式接入 GPT-Image-2,开启多模态 AI 生成全新纪元
大数据·开发语言·数据库·人工智能·gpt·php
JSON_L17 小时前
PHP 使用天地图
php·fastadmin
a里啊里啊17 小时前
软考-软件评测师:知识点整理(四)——信息安全知识
服务器·网络·计算机网络·php·哈希算法·软考·加密算法
weixin_4307509317 小时前
部署FreeRadius+php+apache+mariaDB+daloradius 实现认证计费功能
php·apache·mariadb·daloradius·freeradius
niucloud-admin17 小时前
PHP SAAS 框架常见问题——页面装修本地开发环境配置
php
Johnstons1 天前
丢包率不高但应用仍然卡顿?一次基于 tcpdump +RTT抽样的网络性能排障实战
网络·wireshark·php·tcpdump
eggwyw1 天前
PHP搭建开发环境(Windows系统)
开发语言·windows·php
niucloud-admin1 天前
PHP SAAS 框架常见问题——如何关闭开发者调试模式
php
niucloud-admin1 天前
PHP SAAS 框架常见问题——无法正常打开 admin 后台
php