-1

我在下面的普通 SQL 中有一个查询,我想将其更改为工匠修补程序查询,

select count(P.id), DATEDIFF(P.tarehe_ya_kujifungua,P.tarehe_ya_kuzaliwa) as 
umri from postnatals P INNER JOIN register13s R ON P.namba_ya_kadi_RCH4 = 
R.Namba_ya_kadi WHERE EXTRACT(MONTH FROM tarehe_ya_hudhurio) = EXTRACT(MONTH 
FROM '2018-06-12') group by  P.id HAVING count(DISTINCT R.hudhurio) > 1 AND 
umri >= 7305;

我有一个桌子模型Postnatal.php,我也有桌子模型。postnatalsRegister13.phpregister13s

4

1 回答 1

1

这就是如何将此查询转换为 Laravel 查询构建器的方法。

DB::table('postnatals as P')
    ->innerJoin('register13s as R', 'P.namba_ya_kadi_RCH4','=', 'R.Namba_ya_kadi')
->whereRaw('EXTRACT(MONTH FROM tarehe_ya_hudhurio) = EXTRACT(MONTH FROM '2018-06-12')')
->groupBy('P.id')
->having(DB::raw('count(DISTINCT R.hudhurio)'),'>',1)
->having('umri', '>', 7305)
->selectRaw('select count(P.id), DATEDIFF(P.tarehe_ya_kujifungua,P.tarehe_ya_kuzaliwa) as umri')
->get()

希望这可以帮助。

于 2018-06-14T04:50:28.493 回答