1

I wants to select the count of votes got for each items.Following query works fine but there is a problem if there is no vote for an item then that item is not showing in result.I actually wants details of each items and votes.If there is no vote for an item it should be shown count zero. How can i achieve it?

DB::table('wonders')
        ->leftjoin('vote','votes.wonderId','=','wonders.id')
        ->select('wonders.id','wonders.title',DB::raw('count(votes.vote) as votes'))
        ->get();

3 Answers 3

1

Try this

DB::table('wonders')
            ->leftjoin('vote','votes.wonderId','=','wonders.id')
            ->select('wonders.id','wonders.title',DB::raw('ifnull(count(votes.vote),0) as votes'))
            ->groupBy('wonders.id')
            ->get();
Sign up to request clarification or add additional context in comments.

Comments

0

maybe you need to use iffnull,try this

DB::table('wonders')
        ->leftjoin('vote','votes.wonderId','=','wonders.id')
        ->select('wonders.id','wonders.title',DB::raw('ifnull(count(votes.vote),0) as votes'))
        ->get();

1 Comment

No effect the same result is getting .It only returns the items having atleat 1 vote
0

Try this

DB::table('wonders')
    ->leftjoin('vote','votes.wonderId','=','wonders.id')
    ->selectRaw('wonders.id','wonders.title',count(votes.vote) as votes)
    ->groupBy('wonders.id')
    ->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.