将select查询从mysql转换为laravel查询生成器

osh3o9ms  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(319)

这个mysql查询的laravel查询生成器代码是什么;

SELECT * 
FROM Student
WHERE family_id IN (SELECT family_id 
                    FROM students 
                    GROUP BY family_id 
                    HAVING COUNT(1)>1)
ORDER BY family_id
jhiyze9q

jhiyze9q1#

在where子句中使用函数:

Student::whereIn('family_id', function($query) {
   $query->select('family_id')
     ->from(with(new Students)->getTable())
     ->groupBy('family_id')
     ->havingRaw('COUNT(1) > 1')
})->get();
m3eecexj

m3eecexj2#

$query = DB::select("SELECT * 
FROM Student
WHERE family_id IN (SELECT family_id 
                    FROM students 
                    GROUP BY family_id 
                    HAVING COUNT(1)>1)
ORDER BY family_id")

基于laravel文档

相关问题