-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;
不夠清楚你想要做什麼,請編輯,這是什麼結果? – delboy1978uk
你爲什麼要查詢以你期望的格式給你數據? – SMA
@ delboy1978uk結果會像是顯示的json – John