php - 如何得到一个案例的总和?

标签 php mysql

这是我的案例:

$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);

我想要所选字段的总和,例如:

vote=not bad, customer_comment='' ==> 0 point

vote=bad, customer_comment='' ==> -1 point

vote!="", moderator_view= negative ==> -1 point

总计应为 -2

最佳答案

如果 tablesiterelation 之间或 relationjob_list 之间不存在一对一关系,则此查询将返回意外结果,因为您检索 relation.comments 时未将其添加到 GROUP BY 子句中。

SELECT 中删除 relation.comments 以获得正确的总和。

关于php - 如何得到一个案例的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33873313/

相关文章:

php - 使用 php 从外部 url 获取 application/ld+json

php - MySQL:多条件查询

php - 对数据库数据进行长轮询?

python - 通过 Python 的 subprocess 模块运行 mysqldump 既慢又冗长

mysql - 如何使用 DESCRIBE 防止 mysql 表在 Windows cmd 中换行?

php - SQL Query 找不到获取结果的方法

php - Woocommerce - 根据运输方式计算税收

javascript - 填写下拉菜单和单选按钮选项以继续访问正确的网址

mysql - 如何将数据添加到从另一个表获取的一组列中?

MYSQL 查询帮助- Substring 和 Instring