gpt4 book ai didi

mysql - 如何在 Laravel Eloquent 中使用 MIN 和 MAX sql 查询

转载 作者:行者123 更新时间:2023-11-29 01:25:56 25 4
gpt4 key购买 nike

我想在同一行使用 laravel elaquent minmax,这可能吗?

Appointmentsetting::where('Day','=',1)
->whereIn('PersonID', function ($query) {
$query->select('p.id')
->from('users as p')
->join('appointmentsettings as aps', 'aps.PersonID', '=', 'p.id')
->where('p.active', '=', 1)
->where('aps.CompanyID', '=', 1)
->orWhereIn('aps.PersonID', function ($query2) {
$query2->select('cps.user_id')
->from('companypersonstructs as cps')
->where('cps.CompanyID', '=', 1);
})
->groupBy('aps.PersonID');
})
->where('active', '=', 1)
->select(\DB::raw("SELECT MIN(StartFrom) AS StartFrom, MAX(EndTo) AS EndTo"));
->get();

我有错误

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT MIN(StartFrom) AS StartFrom, MAX(EndTo) AS EndTo from `appointmentsetting' at line 1

最佳答案

->selectRaw(" MIN(StartFrom) AS StartFrom, MAX(EndTo) AS EndTo");

请将查询放在上面并尝试。查询中的主要问题是您使用了两次 select 不需要在 select 或 selectRaw 中写入 select有关原始查询的更多信息,请单击 here

关于mysql - 如何在 Laravel Eloquent 中使用 MIN 和 MAX sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55532665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com