一种更简洁的方法来计算 mysql 数据库的调查响应
目前我使用此代码来计算调查回复:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以尝试这个SQL(我没有测试过) -
要将结果输入PHP,你可以尝试这样的事情(我无法测试PHP代码,所以我基于此代码编写了下面的代码 - http://php.net/manual/en/function.mysql-query.php并祝愿最好的结果!下面的代码几乎肯定会包含错误,但希望能给您带来帮助一个关于如何做到这一点的想法)
You could try this SQL (which I haven't tested) -
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)