获取从MySQL的不同查询中选择的值的总和 - PHP

时间:2015-02-16 11:20:23

标签: php mysql

我想得到这个查询得到的平均值

这是我的查询

$query1 = mysqli_query($conn, "SELECT COUNT(title) as count_title, SUM(average) as total_average FROM table_1 WHERE name='name1' GROUP BY title");
$query2 = mysqli_query($conn, "SELECT COUNT(title) as count_title, SUM(average) as total_average FROM table_1 WHERE name='name2' GROUP BY title");

我的table_1

id     average     title     name 

 1         5       title_aa  name1
 2         4       title_aa  name1
 3         4       title_ab  name1
 4         3       title_ac  name1
 5         2       title_aa  name2
 6         1       title_ab  name2
 7         2       title_ab  name2

PHP

while($values = mysqli_fetch_array($query1)){
    $count_title = $values['count_title'];
    $total_average = $values['total_average'];
    $total = $total_average / $count_title;
    $title = $values[2];

    echo "Title:" . $title . "<br>";
    echo "Average:" . $total . "<br>";
}

while($values2 = mysqli_fetch_array($query2)){
    $count_title2 = $values2['count_title'];
    $total_average2 = $values2['total_average'];
    $total2 = $total_average2 / $count_title2;
    $title2 = $values2[2];

    echo "Title:" . $title2 . "<br>";
    echo "Average:" . $total2 . "<br>";
}

$ query1的输出

标题:title_aa

平均值:4.5

标题:title_ab

平均值:4.0

标题:title_ac

平均值:3.0

$ query2的输出

标题:title_aa

平均值:2.0

标题:title_ab

平均值:1.5

渴望输出

标题:title_aa

平均值:3.25 //总计(4.5 + 2.0)/ 2

标题:title_ab

平均值:2.75 //总计(4.0 + 1.5)/ 2

标题:title_ac

平均值:3.0

2 个答案:

答案 0 :(得分:1)

将第一个和第二个结果的值保存在两个不同的数组中。 喜欢第一次查询

$data=array();
while($values = mysqli_fetch_array($query1)){
    $count_title = $values['count_title'];
    $total_average = $values['total_average'];
    $total = $total_average / $count_title;
    $title = $values[2];
    $data[]=$total;
    echo "Title:" . $title . "<br>";
    echo "Average:" . $total . "<br>";
}

和第二次查询

$data2=array();
while($values2 = mysqli_fetch_array($query2)){
    $count_title2 = $values2['count_title'];
    $total_average2 = $values2['total_average'];
    $total2 = $total_average2 / $count_title2;
    $title2 = $values2[2];
    $data2[]=$total2;
    echo "Title:" . $title2 . "<br>";
    echo "Average:" . $total2 . "<br>";
}

现在使用for循环

for($i=0;$i<sizeof($data);$i++)
{
 $avg=($data[$i]+$data2[$i])/2;
 echo $avg;
}

答案 1 :(得分:1)

  1. 您可以使用AVG()函数
  2. 替换以下两个查询

    mysql代码:

     SELECT title, AVG(average) average FROM table_1 WHERE name='name1' GROUP BY title
     SELECT title, AVG(average) average FROM table_1 WHERE name='name2' GROUP BY title
    
    1. 因此,您可以使用一个请求获得平均值:
    2. mysql代码:

      SELECT t1.title, (AVG(t1.average)+AVG(t2.average))/2
         FROM table_1 t1 JOIN table_1 t2 ON t1.title=t2.title
         WHERE t1.name='name1' AND t2.name='name2'
         GROUP BY t1.title;
      
      1. 编辑,在第一条评论之后
      2. 上一个请求不包含不属于name1和name2的元素。您可以这样做,执行以下操作:

         SELECT title,
          (IFNULL(AVG(IF(t1.name='name1', average, NULL)), 0) 
           + IFNULL(AVG(IF(t1.name='name2', average, NULL)), 0))
           /COUNT(DISTINCT name)
         FROM table_1 t1
         GROUP by title;