简体   繁体   English

如何在 Laravel Eloquent 中编写一个 select 查询,其中 DISTINCT 嵌套在 COUNT 中?

[英]How do I write a select query in Laravel Eloquent with DISTINCT nested in COUNT?

How can I achieve this is Eloquent:我怎样才能做到这一点是Eloquent:

select count(distinct(SID)) from history where `Date` < '2020-08-19'

You can try你可以试试

History::distinct()->where('Date','<','2020-09-19')->count()

for choosing a specific column provide the column name inside the distinct function用于选择特定列提供不同 function 内的列名

distinct('SID')

if your Model is named History, i have no idea whats your model name is:)如果您的 Model 被命名为 History,我不知道您的 model 名称是什么:)

$data = History::where('Date','<',''2020-09-19)->count();

In model onetomany relation https://laravel.com/docs/7.x/eloquent-relationships在 model 一对多关系https://laravel.com/docs/7.x/eloquent-relationships

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM