1

what would be Laravel Query Builder code of this mysql query;

SELECT * 
FROM Student
WHERE family_id IN (SELECT family_id 
                    FROM students 
                    GROUP BY family_id 
                    HAVING COUNT(1)>1)
ORDER BY family_id
1
  • I recommend you to use Eloquen Models. Check out the docs Commented Nov 24, 2018 at 2:22

2 Answers 2

1
$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")

Based on Laravel Documentation

Sign up to request clarification or add additional context in comments.

Comments

0

Use function in whereIn clause:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.