在JOIN表上创建COUNT

时间:2017-01-14 23:33:10

标签: mysql

我有一个查询,我想要计算行数。问题是当我尝试计算完全相同的查询它打印9但是当我运行查询并打印行时它只有5(应该是)

以下是可行的查询并显示5行:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AnimationTest extends JPanel{

int height = 1;

AnimationTest() {
    setPreferredSize(new Dimension(1000,618));
    ActionListener al = (ActionEvent ae) -> {
        repaint();
        if(height < 190) {
            height ++;
        }
    };
    Timer timer = new Timer(10,al);
    timer.start();
}

public void paintComponent(Graphics g) {
    Color c = (Color.BLACK);
    g.setColor(c);
    g.fillRect(0, 309 - height, 1000, 2*height);
}

public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame window = new JFrame("");
            window.getContentPane().add(new AnimationTest());
            createWindow(window);
        }
    });
}

public static void createWindow(JFrame window){
    window.pack();
    window.setLocationByPlatform(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}


}

上面的代码就像它应该的那样,但是当我使用它时:

$results_quiz = $pdo->query("SELECT 
    sr.statistic_ref_id, 
    sr.quiz_id, 
    sr.user_id, 
    sr.total_time, 
    qm.name AS q_name, 
    qm.category_id,
    qm.subcategory_id,
    sc.sub_category_id,
    sc.sub_category_name AS quiz_type,
    pl.time,
    pl.points,
    COUNT(qs.correct_count) AS count_correct 
    FROM pro_quiz_statistic_ref AS sr 
    JOIN pro_quiz_master qm ON qm.id = sr.quiz_id 
    JOIN pro_quiz_subcategory sc ON sc.sub_category_id = qm.subcategory_id
    JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$get_id'
    JOIN pro_quiz_statistic qs ON qs.statistic_ref_id = sr.statistic_ref_id
    WHERE 
    sr.user_id = '$get_id' AND 
    qs.correct_count = '1'
    GROUP BY     
    sr.statistic_ref_id,
    qs.correct_count
  ORDER BY qm.name ASC"); 

打印“9”。谁知道为什么?

1 个答案:

答案 0 :(得分:0)

我通过将表拆分为两个查询/表来找到另一种解决方案。其中一个显示超过0(至少1)正确答案,另一个显示0正确答案。这样它也更符合我的需要。