dsfs23434 2017-08-31 11:42
浏览 11
已采纳

Chart JS的SQL查询

I have the a table like this.

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

How would I query for the result as follows?

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

Currently what I have is by looping the query from 1 to 6. Is there a way for me to query the counts for all possible value of option_id without knowing about the possible values (1 to 6)?

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

2条回答 默认 最新

  • duangenshi9836 2017-08-31 11:50
    关注

    A valid query could be:

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

    Everything else can be handled in application code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改