Bladeren bron

SQL-Query-Fix

Daniel Müllers 11 jaren geleden
bovenliggende
commit
e6f7f24ac8
1 gewijzigde bestanden met toevoegingen van 8 en 14 verwijderingen
  1. 8 14
      app/routes.php

+ 8 - 14
app/routes.php

@@ -18,13 +18,10 @@ Route::get('/', function()
 	$news = News::aktuell()->get();
 
 	$topfilm = DB::table(DB::raw('film_films'))
-	->select(DB::raw('film_films.*, COUNT(film_u.id) as upvotes, COUNT(film_d.id) as downvotes, COUNT(film_u.id) - COUNT(film_d.id) as vcount'))
-	->leftJoin('votes as film_u', function($join) {
-		$join->on('u.film', '=', 'films.id')->on('u.stimme', 'IS', DB::raw('TRUE'));
-	})
-	->leftJoin('votes as film_d', function($join) {
-		$join->on('d.film', '=', 'films.id')->on('d.stimme', 'IS', DB::raw('FALSE'));
-	})
+	->select(DB::raw('film_films.*, COUNT(case when film_votes.stimme IS TRUE then 1 end) as upvotes, 
+		COUNT(case when film_votes.stimme IS FALSE then 1 end) as downvotes, 
+		COUNT(case when film_votes.stimme IS TRUE then 1 end) - COUNT(case when film_votes.stimme IS FALSE then 1 end) as vcount'))
+	->leftJoin('votes', 'votes.film', '=', 'films.id')
 	->whereNull('films.gesehen')
 	->groupBy('id')
 	->orderBy('vcount', 'DESC')->orderBy('vorgeschlagen', 'ASC')
@@ -153,13 +150,10 @@ Route::get('logout', array('as' => 'logout', function() {
 Route::get('vorgeschlagen', function() {
 	
 	$filme = DB::table(DB::raw('film_films'))
-	->select(DB::raw('film_films.*, COUNT(film_u.id) as upvotes, COUNT(film_d.id) as downvotes, COUNT(film_u.id) - COUNT(film_d.id) as vcount'))
-	->leftJoin('votes as film_u', function($join) {
-		$join->on('u.film', '=', 'films.id')->on('u.stimme', 'IS', DB::raw('TRUE'));
-	})
-	->leftJoin('votes as film_d', function($join) {
-		$join->on('d.film', '=', 'films.id')->on('d.stimme', 'IS', DB::raw('FALSE'));
-	})
+	->select(DB::raw('film_films.*, COUNT(case when film_votes.stimme IS TRUE then 1 end) as upvotes, 
+		COUNT(case when film_votes.stimme IS FALSE then 1 end) as downvotes, 
+		COUNT(case when film_votes.stimme IS TRUE then 1 end) - COUNT(case when film_votes.stimme IS FALSE then 1 end) as vcount'))
+	->leftJoin('votes', 'votes.film', '=', 'films.id')
 	->whereNull('films.gesehen')
 	->groupBy('id')
 	->orderBy('vcount', 'DESC')->orderBy('vorgeschlagen', 'ASC')