SQL:在同一行上显示多个select count()

时间:2015-09-29 06:39:40

标签: php mysql arrays

我需要计算一系列变量,然后将结果存储在数组中。 这就是我接触它的方式。

function statsQuery($array, $column, $table, $columnDate, $minDate, $maxDate) {
    global $query;
    $query = "select ";
    $index = 0;
    $flag = count($array);
    foreach ($array as $element) {
        $index++;
        if ($index == $flag) {
            $query .= "count(case when " . $column . " = '" . $element . "' then " . $column . " else null end) as " . $column . "_" . preg_replace('/\s+/', "_", $element) . " ";
        } else {
            $query .= "count(case when " . $column . " = '" . $element . "' then " . $column . " else null end) as " . $column . "_" . preg_replace('/\s+/', "_", $element) . ", ";
        }
    }
    $query .= " from " . $table;
    $query .= " where " . $columnDate . " between '" . $minDate . "' and '" . $maxDate . "'";
}

statsQuery($genderSection, 'gender', 'candidate', 'candidate_date_introduced', $minDate, $maxDate);

我们说我有这个阵列:

$genderSection= array('masculine', 'feminine', 'unspecified');

可能的输出可以是:

gender_masculine |  gender_feminine  |  gender_unspecified   | 
--------------------------------------------------------------  
420              | 512               | 23                    |

但是,我需要处理的数据包含这种类型的其他几个变量。例如:

$levelOptions = array('low', 'good', 'acceptable', 'exeptional');

我想实现这样的目标:

gender_masculine |  gender_feminine  |  gender_unspecified   |  level_low |  level_good |  level_acceptable  |  level_exepctional   |  
------------------------------------------------------------------------------------------------------------------------------------- 
xx               | xx                | xx                    |  xx        |  xx         |  xx                |  xx                  |

我被困在SQL部分。我无法弄清楚如何使SQL返回同一行的结果。我想出的最佳选择是使用上面的脚本为每个变量运行单独的查询。我试过UNION,JOIN,但没有运气。

我的问题是:

  • 如何查询同一个表或不同的表并将count(case)作为一行?我认为如果我可以将所有内容存储在一个数组中,那么使用foreach操作和输出数据会更容易。
  • 将所有内容作为一行获得性能效率,还是应该坚持我当前的方法并独立地为每个变量使用该函数?

对不起,如果我的问题很愚蠢,我真的尝试了围绕这个主题找到的所有内容。任何帮助将不胜感激。

添加了表格结构。数据通过HTML <select></select>引入。

id  |  gender       |  level        |
-------------------------------------
1   |  feminine     |  low          |
2   |  mascline     |  good         |
3   |  unspecified  |  acceptable   |
4   |  feminine     |  exceptional  |
.....................................
n   |  feminine     |  exceptional  |

2 个答案:

答案 0 :(得分:1)

尝试使用此格式的查询

SELECT 
count(*) AS total,
SUM(CASE WHEN gender = 'masculine' THEN 1 ELSE 0 END) AS gender_masculine,
SUM(CASE WHEN gender = 'feminine' THEN 1 ELSE 0 END) AS gender_feminine,
SUM(CASE WHEN gender = 'unspecified' THEN 1 ELSE 0 END) AS gender_unspecified,
SUM(CASE WHEN level = 'low' THEN 1 ELSE 0 END) AS level_low,
SUM(CASE WHEN level = 'good' THEN 1 ELSE 0 END) AS level_good,
SUM(CASE WHEN level = 'acceptable' THEN 1 ELSE 0 END) AS level_acceptable,
SUM(CASE WHEN level = 'exceptional' THEN 1 ELSE 0 END) AS level_exceptional
from table

答案 1 :(得分:1)

您可以尝试如下:

SELECT SUM( IF( gender =  'masculine', 1, 0 ) ) AS gender_masculine, SUM( IF( gender =  'feminine', 1, 0 ) ) AS gender_feminine, SUM( IF( gender =  'unspecified', 1, 0 ) ) AS gender_unspecified, SUM( IF( nlevel =  'low', 1, 0 ) ) AS level_low, SUM( IF( nlevel =  'good', 1, 0 ) ) AS level_good, SUM( IF( nlevel =  'acceptable', 1, 0 ) ) AS level_acceptable, SUM( IF( nlevel =  'exceptional', 1, 0 ) ) AS level_exceptional FROM a_temp

第二种方法: 您也可以尝试:

SELECT  `nlevel` AS tags, COUNT(  `nlevel` ) AS totals FROM a_temp GROUP BY  `nlevel` UNION ALL SELECT gender AS tags, COUNT( gender ) AS totals FROM a_temp GROUP BY  `gender`