Good afternoon,
I’m trying to execute this query with Laravel:
SELECT COUNT(id) as total, (SUM(score) / COUNT(id)) as average,
(SELECT COUNT(id) FROM rates Where book_id = :book_id AND score >= 1 AND score < 2) as count_1,
(SELECT COUNT(id) FROM rates Where book_id = :book_id AND score >= 2 AND score < 3) as count_2,
(SELECT COUNT(id) FROM rates Where book_id = :book_id AND score >= 3 AND score < 4) as count_3,
(SELECT COUNT(id) FROM rates Where book_id = :book_id AND score >= 4 AND score < 5) as count_4,
(SELECT COUNT(id) FROM rates Where book_id = :book_id AND score = 5) as count_5
FROM rates
WHERE book_id = :book_id;
I have been looking for Away in Laravel to execute subqueries in the Select Statement. My last intent it has been with:
return DB::table('rates')
->selectRaw([
DB::raw('COUNT(id) as total'),
DB::raw('(SUM(score) / COUNT(id)) as average'),
'(' . DB::raw(DB::table('rates')->select(DB::raw('COUNT(id) as count_1'))->where('book_id', $bookId)->where('score', '>=', 1)->where('score', '<', 2)->toSql()) . ')',
'(' . DB::raw(DB::table('rates')->select(DB::raw('COUNT(id) as count_2'))->where('book_id', $bookId)->where('score', '>=', 2)->where('score', '<', 3)->toSql()) . ')',
'(' . DB::raw(DB::table('rates')->select(DB::raw('COUNT(id) as count_3'))->where('book_id', $bookId)->where('score', '>=', 3)->where('score', '<', 4)->toSql()) . ')',
'(' . DB::raw(DB::table('rates')->select(DB::raw('COUNT(id) as count_4'))->where('book_id', $bookId)->where('score', '>=', 4)->where('score', '<', 5)->toSql()) . ')',
'(' . DB::raw(DB::table('rates')->select(DB::raw('COUNT(id) as count_5'))->where('book_id', $bookId)->where('score', 5)->toSql()) . ')'
])
->where('book_id', $bookId)->get();
But I get errors from Laravel.
Do you know how to execute a MySQL query with subqueries in the select statement, using the Query BUilder of Laravel?
(And of course, I have been searching for 1 hour how to do and did not find any good answer on internet and Stack Overflow).
2
Answers
Following @xenooooo advice in the comments of the post, I check the page of "advanced subqueries" I couldn't find it by my own and writed this function using
addSelect
:Note that I use
first
because I expect only one resultI’d write the query this way, so it doesn’t use any subqueries, and scans the table only once:
I’m not sure about Laravel syntax, but it might be something like this: