I'm trying to run this query
$revs = DB::table('reviews')->where('website_id', $website_id)
->selectRaw('
COUNT(id) AS total_count,
COUNT(id) AS third_party_reviews_count WHERE review_source_id!=1,
COUNT(id) AS normal_reviews_count WHERE review_source_id=1,
COUNT(id) AS total_1_star WHERE stars = 1,
COUNT(id) AS total_2_star WHERE stars = 2,
COUNT(id) AS total_3_star WHERE stars = 3,
COUNT(id) AS total_4_star WHERE stars = 4,
COUNT(id) AS total_5_star WHERE stars = 5')->get();
but it says
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'COUNT(id) AS normal_reviews_count WHERE `review_source_id`=1,COUNT(id) AS
I tried using online SQL syntax checkers but none gave me a solution to the problem.
My Answer:
>select(DB::raw('COUNT(*) as total_reviews, AVG(rating) as avg_rating'))
->where('product_id', $product_id)
->get();
Rate this post
4 of 5 based on 8128 votesComments