mysql - how to count percentage in laravel -


i'm working on feedback system user can give feedback, in case, have count percentage each option question has many options should show percentage each option, example, how many people selected option a,b,c,d on.

my answers table

     user_id         question_id      option(selected user)      1                       1                             2      2                       1                             1      3                       1                             4      4                       1                             3 

now want achieve is

  question1      option1(25% people selected option1)      option2(25% people selected option2)      option3(25% people selected option3)      option4(25% people selected option4)  

what should query above output,can 1 on please.

try one,

you percentage of each option question question_id = 1

db::table('answers')->where('question_id',1)     ->select('option',db::raw('count(*) *100 / (select count(*) answers) count'))     ->groupby('option')     ->get(); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -