dongzhabo2796 2013-07-04 08:23
浏览 7
已采纳

按ID分组不在php中分组

im having a bit of a problem with my php script that counts the id column entries that match a certain questionnaire id. The results seem to be quite antisocial and just dont want to group up for some reason....

        $sql30 = <<<SQL
    SELECT id, COUNT(id) 
    FROM `QuestionnaireAnswers`
    WHERE questionnaireID='$questionnaireID'
    GROUP BY id
    SQL;
    if(!$result30 = $db->query($sql30)){ die('There was an error running the query [' . $db->error . ']');}
    while($row30 = $result30->fetch_assoc()){
        if ($row30['COUNT(id)'] == '' OR $row30['COUNT(id)'] == '0'){$numberofresponses = '0';}
        else {$numberofresponses = $row30['COUNT(id)'];}    
    echo '<td>'.$numberofresponses.'</td>';
    }

If I use a sample questionnaireID of 1327809154 and run the following query in phpmyadmin :

SELECT id, COUNT( id ) 
FROM  `QuestionnaireAnswers` 
WHERE questionnaireID =  '1327809154'
GROUP BY id
LIMIT 0 , 30

I get the following results :

id  COUNT(id)
129 1
130 1
131 1
132 1
133 1
134 1
277 1
278 1
280 1
281 1
282 1
284 1
290 1
419 1
424 1
425 1
426 1

so.....it appears to be counting the results, but not grouping them to give me a total.........

EDIT,I have now changed it to :

        $sql30 = <<<SQL
    SELECT questionnaireID, COUNT(questionnaireID) 
    FROM `QuestionnaireAnswers`
    WHERE questionnaireID='$questionnaireID'
    GROUP BY questionnaireID
    SQL;
    if(!$result30 = $db->query($sql30)){ die('There was an error running the query [' . $db->error . ']');}
    while($row30 = $result30->fetch_assoc()){
        if ($row30['COUNT(questionnaireID)'] == '' OR $row30['COUNT(questionnaireID)'] == '0'){$numberofresponses = '0';}
        else {$numberofresponses = $row30['COUNT(questionnaireID)'];}   
    echo '<td>'.$numberofresponses.'</td>';
    }

This returns :

questionnaireID COUNT(questionnaireID)
 1327809154 17

this works in phpmyadmin, but some reason nothing is returned in my script, the table column where the count should be is blank.

  • 写回答

4条回答 默认 最新

  • drw85135 2013-07-04 08:25
    关注

    Your IDs are all unique, so they cannot be grouped. Only equal values are grouped together and their count summed up.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看