一种更简洁的方法来计算 mysql 数据库的调查响应

发布于 2024-11-24 10:06:10 字数 1641 浏览 1 评论 0原文

目前我使用此代码来计算调查回复:

$VERYHAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'VERY HAPPY' ");
$HAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'HAPPY' ");
$DONTKNOW = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'DONT KNOW' ");
$UNHAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'UNHAPPY' ");
$VERYUNHAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'VERY UNHAPPY' ");

 $VERYHAPPYcount = mysql_num_rows($VERYHAPPY);
 $HAPPYcount = mysql_num_rows($HAPPY);
 $DONTKNOWcount = mysql_num_rows($DONTKNOW); 
 $UNHAPPYcount = mysql_num_rows($UNHAPPY);
 $VERYUNHAPPYcount = mysql_num_rows($VERYUNHAPPY); 
  echo "VERY HAPPY = $VERYHAPPYcount</br>HAPPY = $HAPPYcount</br>DON'T KNOW = $DONTKNOWcount</br>UNHAPPY = $UNHAPPYcount</br>VERY UNHAPPY = $VERYUNHAPPYcount";  

如您所见 - 这真的很混乱!有更好的方法来做到这一点吗?我尝试过 COUNT 和 GROUP BY,但无法让它们工作。谢谢

currently I using this code to count survey responses:

$VERYHAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'VERY HAPPY' ");
$HAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'HAPPY' ");
$DONTKNOW = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'DONT KNOW' ");
$UNHAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'UNHAPPY' ");
$VERYUNHAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'VERY UNHAPPY' ");

 $VERYHAPPYcount = mysql_num_rows($VERYHAPPY);
 $HAPPYcount = mysql_num_rows($HAPPY);
 $DONTKNOWcount = mysql_num_rows($DONTKNOW); 
 $UNHAPPYcount = mysql_num_rows($UNHAPPY);
 $VERYUNHAPPYcount = mysql_num_rows($VERYUNHAPPY); 
  echo "VERY HAPPY = $VERYHAPPYcount</br>HAPPY = $HAPPYcount</br>DON'T KNOW = $DONTKNOWcount</br>UNHAPPY = $UNHAPPYcount</br>VERY UNHAPPY = $VERYUNHAPPYcount";  

as you can see - it's really messy! Any better way to do this? I've tried COUNT and GROUP BY, but can't get them to work. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2024-12-01 10:06:10

你可以尝试这个SQL(我没有测试过) -

SELECT count(*) as recordcount, response 
FROM answers, complete 
WHERE (answers.uniqueID = complete.uniqueID) 
AND (complete.timestamp) IS NOT NULL 
AND (answers.surveyID='$surveyID') 
AND QID = '$QID'
GROUP BY response

要将结果输入PHP,你可以尝试这样的事情(我无法测试PHP代码,所以我基于此代码编写了下面的代码 - http://php.net/manual/en/function.mysql-query.php并祝愿最好的结果!下面的代码几乎肯定会包含错误,但希望能给您带来帮助一个关于如何做到这一点的想法)

//assign sql above to $query variable prior to doing this
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
    echo $row['response'] & " = " & $ row['recordcount'] & "count</br>"
}

You could try this SQL (which I haven't tested) -

SELECT count(*) as recordcount, response 
FROM answers, complete 
WHERE (answers.uniqueID = complete.uniqueID) 
AND (complete.timestamp) IS NOT NULL 
AND (answers.surveyID='$surveyID') 
AND QID = '$QID'
GROUP BY response

To get the results into PHP you could try something like this (I can't test PHP code so I've based the code below on this code - http://php.net/manual/en/function.mysql-query.php and hoped for the best! The code below will almost certainly contain errors but will hopefully give you an idea of how it could be done)

//assign sql above to $query variable prior to doing this
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
    echo $row['response'] & " = " & $ row['recordcount'] & "count</br>"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文