Created
January 5, 2022 02:59
-
-
Save bran921007/c71b6fc18e679a63e8f7bd34da94aaae to your computer and use it in GitHub Desktop.
add conditional aggregate in mysql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Course::query() | |
| ->select('courses.*') | |
| ->addSelect([ | |
| "lessons count' => Lesson::query () | |
| ->select (DB: : raw( 'COUNT (lessons. id) ')) | |
| ->whereColumn ('lessons.course id','courses. id'), | |
| 'total_length' => Lesson::query() | |
| ->select (DB: : raw ('SUM (lessons. video_length) ')) | |
| ->whereColumn('lessons. course_id', 'courses.id') , | |
| ]) | |
| ->orderBy('title") | |
| ->paginate(20) | |
| ->withQueryString() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment