从正常的sql查询构建工匠查询

时间:2018-06-14 04:35:17

标签: php laravel artisan laravel-5.6 tinker

我在下面的普通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表的模型postnatals,我Register13.phpregister13s

1 个答案:

答案 0 :(得分:1)

如何将此查询转换为Laravel Query构建器。

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()

希望这有帮助。