2017-08-31 37 views
-1

我有這樣的表格。圖表JS的SQL查詢

| id | question_id | option_id | 
--------------------------------- 
| 1 |   1 |   1 | 
| 1 |   1 |   2 | 
| 1 |   1 |   3 | 
| 1 |   1 |   2 | 
| 1 |   1 |   2 | 
| 1 |   1 |   6 | 

如何查詢結果如下?

[ 
    {'1' => 1}, 
    {'2' => 3}, 
    {'3' => 1}, 
    {'4' => 0}, 
    {'5' => 0}, 
    {'6' => 1}, 
] 

目前我有什麼是循環從1到6,查詢是否有適合我的方式來查詢option_id的所有可能的值的計數不知道有關可能的值(1〜6)?

Select sum(case when option_id = 1 then 1 else 0 end) as "1" FROM fcs_answer WHERE question_id = 4; 
+0

不夠清楚你想要做什麼,請編輯,這是什麼結果? – delboy1978uk

+0

你爲什麼要查詢以你期望的格式給你數據? – SMA

+0

@ delboy1978uk結果會像是顯示的json – John

回答

1

有效查詢可能是:

SELECT option_id, COUNT(*) FROM my_table GROUP BY option_id; 

事都可以在應用程序代碼

0

嘗試此查詢處理。我認爲它是有用的

SELECT `option_id `,SUM(1) FROM table_name 
GROUP BY `option_id `