Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 添加属性";“在飞行中”;在mySQL查询期间_Php_Mysql - Fatal编程技术网

Php 添加属性";“在飞行中”;在mySQL查询期间

Php 添加属性";“在飞行中”;在mySQL查询期间,php,mysql,Php,Mysql,我有一个如下的查询: SELECT avg(round({$cohort1}/{$cohort2} * 100)) as overall_average, `category`, `value`, {$cohort1},{$cohort2},`group`, `quadrant`,round({$cohort1}/{$cohort2} * 100) as index_value from {$table}; 当我把平均数包括在内时,这并不完全有效。它只返回一行。我需要它返回所有行。如果我去掉

我有一个如下的查询:

SELECT avg(round({$cohort1}/{$cohort2} * 100)) as overall_average,
`category`, `value`, {$cohort1},{$cohort2},`group`,
`quadrant`,round({$cohort1}/{$cohort2} * 100) as index_value 
from {$table};
当我把平均数包括在内时,这并不完全有效。它只返回一行。我需要它返回所有行。如果我去掉平均值,那么所有的结果都很好,但是我没有计算的平均值。不过,我可以在PHP中使用单独的查询来实现这一点

然而,我意识到我需要根据队列的值推导象限(一个字符串)的值。这就是规则。问题是,我不知道如何将其合并到我的查询中

IF (the_value > overall_average and index_value > 100) THEN `quadrant` = "Priority One"
ELSEIF (the_value < overall_average and index_value > 100) THEN `quadrant` = "Potential"
ELSEIF (the_value > overall_average and index_value < 100) THEN `quadrant` = "Basics"
ELSEIF (the_value < overall_average and index_value < 100) THEN `quadrant` = "Ignore"
如果(总平均值>总平均值和指数值>100),则“象限”=“优先级一”
ELSEIF(值<总体平均值和指数值>100)然后`象限`=“潜力”
ELSEIF(值>总体平均值和指数值<100)然后“象限”=“基本”
ELSEIF(指数值<总体平均值和指数值<100)然后“象限”=“忽略”

你能帮我提出一个完整的问题吗?如果数据从数据库返回后更简单,我也愿意在PHP中处理数据。

您可以使用用例子句:

SELECT column1, column2, column2 as alias,..., 
CASE
    WHEN the_value > overall_average and index_value > 100 THEN 'Priority One'
    ...
    WHEN another_condition THEN 'Potential'
    ...
    ELSE 'default value'
END AS quadrant
FROM table

Reference

您是否在sql语句中使用了
groupby
avg()
是一个聚合函数,需要在所有不包含聚合函数的列上使用
groupby
。我是否在我的
选择
更新我的答案之前添加此选项。您应该将大小写表达式作为列添加到SELECTOkay中……我最终得到的是返回的整个内容,而不仅仅是值:
大小写象限,当值>总体平均值和索引值>100,然后是“优先级一”…