这是我的情况:
$con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");
$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
mysql_query("SET CHARACTER SET utf8");
$dbresult=mysql_query("SELECT
tablesite.name,
tablesite.family,
tablesite.phone_number,
job_list.job_name,
relation.comments,
SUM(CASE WHEN action.vote ='perfect' AND action.customer_comment ='' THEN 2
WHEN action.vote ='nice' AND action.customer_comment ='' THEN 1
WHEN action.vote ='not bad' AND action.customer_comment ='' THEN 0
WHEN action.vote ='bad' AND action.customer_comment ='' THEN -1
WHEN action.vote !='' AND action.moderator_view ='negative' THEN -1
WHEN action.vote !='' AND action.moderator_view ='positve' THEN 1
WHEN action.vote !='' AND action.moderator_view ='no change' THEN 0 ELSE 0
END) positive
FROM tablesite
INNER JOIN relation
on tablesite.id_user=relation.user_id
INNER JOIN job_list
on relation.job_id=job_list.job_id
left JOIN action
ON tablesite.id_user=action.service_provider_id
WHERE job_name LIKE '%".$_POST['search']."%'
OR name LIKE '%".$_POST['search']."%'
OR family LIKE '%".$_POST['search']."%'
group by name,family,job_name,phone_number", $con);
我想要所选字段的总和,例如:
投票=不错,customer_comment =''==> 0分
投票=差,customer_comment =''==> -1分
投票!=“”,moderator_view =否定==> -1点
总数应为-2
最佳答案
如果tablesite
和relation
之间或relation
和job_list
之间没有一对一的关系,则此查询将返回意外的结果,因为您正在检索relation.comments
而不将其添加到GROUP BY
子句中。
从relation.comments
中删除SELECT
以获取正确的总和。
关于php - 如何得到一个案件的总和?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33873313/