从多个表中选择计数并分组

时间:2015-05-23 12:17:05

标签: mysql count

帮助我!

   <div class = "parent">
       <div class="count">4</div>
         <button class="view">CLICK ME</button>
    </div>
    <div class = "parent">
       <div class="count">12</div>
         <button class="view">CLICK ME</button>
    </div>



    $(document).ready(function(){
      $('.view').click(function() {
      var count = $(this).closest('.parent').find('.count');
      counter = count.html();
      counter++;
     count.html(counter);
    });
});

您的SQL语法有错误;查看与您的MariaDB服务器版本对应的手册,以便在$sql = SELECT `id`, `email`, `avatar`,`fullname`, SUM(result) as sum_result, SUM(total) as sum_total, COUNT(rank) as `top_1` where `itq_exam_thread_test`.`rank` = 1, COUNT(rank) as `top_2` where `itq_exam_thread_test`.`rank` = 2, COUNT(rank) as `top_3` where `itq_exam_thread_test`.`rank` = 3 GROUP BY userid_created ORDER BY sum_result DESC, sum_rate DESC LIMIT 0, 100'; 附近使用正确的语法。itq_exam_thread_test = 1,

1 个答案:

答案 0 :(得分:1)

WHERE是整个查询的子句,而不是SELECT子句中的修饰符。你想要的是条件聚合。在MySQL中,执行计数非常简单,因为它将布尔表达式视为数字上下文中的整数。因此,您可以使用SUM()

SELECT id, email, avatar, fullname, SUM(result) as sum_result,
       SUM(total) as sum_total,
       SUM(rank = 1) as top_1,
       SUM(rank = 2) as top_2,
       SUM(rank = 3) as top_3
FROM itq_exam_thread_test
GROUP BY userid_created
ORDER BY sum_result DESC
LIMIT 0, 100;

我从sum_rate中删除了ORDER BY,因为它未定义。