Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 2在同一查询中计数_Mysql_Count - Fatal编程技术网

Mysql 2在同一查询中计数

Mysql 2在同一查询中计数,mysql,count,Mysql,Count,我有以下SQL查询(由另一个人编写): 它从调查中输出统计数据。9只是测试用的voteCount返回为每个问题选择答案变量的次数。它工作得很好: q.id q.text v.id voteCount comment for voteCount 10 blahblah 5 2 2 is two times (5 and 5) for question 10 10 blahblah 4 1 1 is one time (4) for qu

我有以下SQL查询(由另一个人编写):

它从调查中输出统计数据。9只是测试用的
voteCount
返回为每个问题选择答案变量的次数。它工作得很好:

q.id q.text    v.id voteCount comment for voteCount
10   blahblah  5    2         2 is two times (5 and 5) for question 10
10   blahblah  4    1         1 is one time (4) for question 10
10   blahblah  2    1         1 is one time (2) for question 10
10   blahblah  5    2         2 is two time (5 and 5) for question 10
11   foobarfoo 5    1         1 is one time (5) for question 11
现在我想修改查询,使其在结果中有另一列,该列等于问题的回答次数

q.id q.text    v.id voteCount totalCount comment for totalCount
10   blahblah  5    2         4          4 is four times question 10 is answered
10   blahblah  4    1         4          4 is four times question 10 is answered
10   blahblah  2    1         4          4 is four times question 10 is answered
10   blahblah  5    2         4          4 is four times question 10 is answered
11   foobarfoo 5    1         1          1 is one time question 11 is answered
我试过这个:

select q.id, q.text, v.id, count(v.id) as voteCount, count(q.id) as totalCount
  from survey as s 
  join sessions as ss on ss.session_state='FINISHED' and ss.survey=s.id
  join answer as a on a.sessionId=ss.id 
  join answer_item as a_i on a_i.answer=a.id 
  join question_variant as v on v.id=a_i.question_variant_id
  join question as q on q.id=v.question_id 
  where s.id=9  
  group by q.id, v.id order by q.id, voteCount desc
但令人惊讶的是,这两列包含相同的值

q.id q.text    v.id voteCount totalCount comment for totalCount
10   blahblah  5    2         2          ???
10   blahblah  4    1         1          ???
10   blahblah  2    1         1          ???
10   blahblah  5    2         2          ???
11   foobarfoo 5    1         1          ???
为什么以及如何解决此问题?

COUNT()
和其他聚合函数是基于每个组应用的。由于q.id和v.id是分组列,因此它们对每个组的计数都将是该组中的数字结果(如果这些列可为空,则在少数组中可能为零)

我不确定我是否理解您试图提取的其他信息,但听起来您希望以不同的方式对结果进行分组,在这种情况下,您可能需要单独的查询。我只能根据你的问题和你的其他问题猜测可能是什么,但可能是这样的:

select q.id, count(*) as answerCount
from survey as s 
  join sessions as ss on ss.session_state='FINISHED' and ss.survey=s.id
  join answer as a on a.sessionId=ss.id 
  join answer_item as a_i on a_i.answer=a.id 
  join question_variant as v on v.id=a_i.question_variant_id
  join question as q on q.id=v.question_id 
where s.id=9  
group by q.id

这是因为您基本上只是在计算组中的行,并且每个ID的组都是相同的。如果您只需要总计,您可以使用
DISTINCT
来计算不同的ID:

select 
  count(distinct v.id) as voteCount, 
  count(distinct q.id) as totalCount
....
但是您也可以通过q.id和v.id进行分组,以获得问题的各个属性(如标题),因此COUNT DISTINCT对您没有任何好处。结果将始终为每一个1。似乎您想要使用分析函数(窗口函数),它存在于Oracle和其他各种数据库中,但不存在于MySQL中

有一些技巧可以模拟这些函数(请参见DBA.SE-),但在本例中,我认为有两个子选择可以为您提供这些技巧:

select 
  q.id, 
  q.text, 
  v.id, 
  (select
    count(vx.id)
  from
    question_variant vx
  where
    vx.id = v.id) as VoteCount,
  (select
    count(qx.id)
  from
    question qx
  where
    qx.id = q.id) as TotalCount
from 
  survey as s 
  join sessions as ss on ss.session_state='FINISHED' and ss.survey=s.id
  join answer as a on a.sessionId=ss.id 
  join answer_item as a_i on a_i.answer=a.id 
  join question_variant as v on v.id=a_i.question_variant_id
  join question as q on q.id=v.question_id 
where 
  s.id=9  
group by 
  q.id, v.id 
order by 
  q.id, 
  voteCount desc
与其使用
分组依据
,不如在此查询中使用
选择distinct
。主要的一点是,您选择所有您喜欢的字段,并且对于每一行,您分别计算该行问题的回答次数以及该行问题变量的投票数

select 
  q.id, 
  q.text, 
  v.id, 
  (select
    count(vx.id)
  from
    question_variant vx
  where
    vx.id = v.id) as VoteCount,
  (select
    count(qx.id)
  from
    question qx
  where
    qx.id = q.id) as TotalCount
from 
  survey as s 
  join sessions as ss on ss.session_state='FINISHED' and ss.survey=s.id
  join answer as a on a.sessionId=ss.id 
  join answer_item as a_i on a_i.answer=a.id 
  join question_variant as v on v.id=a_i.question_variant_id
  join question as q on q.id=v.question_id 
where 
  s.id=9  
group by 
  q.id, v.id 
order by 
  q.id, 
  voteCount desc