如何在sql php中从同一个表中选择多个avg

时间:2017-06-19 16:15:04

标签: php mysql sql where

我正在尝试使用1个表格选择标题的平均评分我已经尝试过当我使用CASE时它不能正常工作当它返回所有平均值但是我需要它只返回从选择的用户中选择的

ID USERNAME TITLE RATING RATED_BY

1                   tommy           Bigbear          5       oscar

2                   tommy           Bigbear          2       dillan

3                   tommy           HardLife         3       jeff

4                   benben          Bigbear          9       mike

我的数据库名为game_ratings

$rates_select = mysqli_query($conn,"SELECT rating AS rates,
       CASE WHEN title ='Bigbear' THEN AVG(rating) ELSE NULL END AS bigbear,
       CASE WHEN title ='HardLife' THEN AVG(rating) ELSE NULL END AS hardlife
       FROM game_ratings WHERE username='tommy'") ;

    if($rate_select  > 0){

      $rate = mysqli_fetch_array($rate_select) ;
      $rate_bigbear = $rate['bigbear'] ;
      $rate_hardlife = $rate['hardlife'] ;

     echo $rate_bigbear ; 
    }

这回归所有tommys评级的平均值不仅仅是大熊 如果我回应$ rate_hardlife我希望它给我平均值如果我回应$ rate_bigbear我希望它给我那个平坦的汤米

2 个答案:

答案 0 :(得分:2)

正确的语法将CASE作为AVG()的参数:

SELECT AVG(CASE WHEN title = 'Bigbear' THEN rating END) AS bigbear,
       AVG(CASE WHEN title = 'HardLife' THEN rating END) AS hardlife
FROM game_ratings
WHERE username = 'tommy';

当然,写起来似乎更简单:

SELECT title, AVG(rating) as rating
FROM game_ratings
WHERE username = 'tommy' AND
      title  IN ('Bigbear', 'HardLife')
GROUP BY title;

但是这会返回两行的值。

答案 1 :(得分:2)

假设<div style="margin-left: 20px;"> <input type="checkbox" id="firstgroup" /> Select All<br> <input type="checkbox" class="order" id="first" /> A<br> <input type="checkbox" class="order" id="second" /> B<br> <input type="checkbox" class="order" id="third" /> C<br> <input type="checkbox" class="order" id="forth" /> D </div> <div id="result"></div> <div id="form"></div> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> $("#firstgroup").click(function() { $('#form').text(''); $('#result').text(''); $('input:checkbox').not(this).prop('checked', this.checked); if($(".order:checked").length == $(".order").length) { $('#form').text('all of them are checked'); } }); $(".order").on('click', function() { var selectedItems = ''; $('#result').text(''); $('#form').text(''); if ($(".order:checked").length == $(".order").length) { $('#firstgroup').prop('checked', true); $('#form').text('all of them are checked'); } else if ($(".order:checked").length > 0) { $('#firstgroup').prop('checked', false); $(".order:checked").each(function(i, d) { selectedItems += d.id + ','; }); $('#result').text('Checked Checkboxes: ' + selectedItems.substring(0, selectedItems.length - 1)); $('#form').text('Total number of checked checkboxes: ' + $(".order:checked").length); } }); 像其他聚合函数一样忽略空值,你应该能够做到这样的事情:

AVG

请注意,我省略了SELECT AVG(CASE WHEN title = 'Bigbear' THEN rating ELSE NULL END) AS bigbear , AVG(CASE WHEN title = 'HardLife' THEN rating ELSE NULL END) AS hardlife FROM game_ratings WHERE username = 'tommy' ; 的{​​{1}}部分;在查询结果中包含该字段是没有意义的(您将从检查的行中获得有效的随机值。)

另外,正如Mark Ba​​ker的评论所暗示的那样,你可能会好得多;他建议的查询结果会自动包含新标题的平均评分。