嵌套SELECT以查找最高值,如果未找到则显示0

时间:2016-05-20 04:30:28

标签: mysql select nested max

谢谢stackoverflow社区!多年来我从你那里学到了很多东西,而且我最近创建了一个帐户。我希望这个问题的答案已经显然已经存在,但是如果我读了另一篇文章,我会睁大眼睛。这是我的问题:

我最近使用嵌套的SELECT从表中获得每个学生的最高分。我是通过另一个帖子教我的小技巧这样做的。我无法找到我从中学到的确切帖子,但这里有一个基本相同的技巧片段。我想,如果你精通sql,那对你来说并不新鲜:

SELECT id, authorId, answer, votes
FROM (  SELECT id, authorId, answer, votes
        FROM answers
        ORDER BY votes DESC) AS h
GROUP BY authorId

ORDER BY ____ DESC创建最后一个值,最高覆盖所有之前的值,所以你最终只得到它......如果我理解正确的话。所以,这很棒,我根据自己的需要量身定做。唯一的问题是,现在,我想再添加一个功能,我将脑细胞放在上面。我希望一些慷慨的人能够理顺我。我想从我的"名单中获得一份完整的学生名单"如果没有给定学生的分数,请在我的"持有人"表,我希望它显示" 0"。这就是我所拥有的,而且我不知道如何调整它来做到这一点:

 SELECT *
 FROM (
      SELECT
      holder.id,
      #IFNULL(holder.score, 0) AS score,
      holder.score AS score,
      holder.total,
      holder.student_id AS stu_id,
      holder.date AS date,
      users.firstname AS first,
      users.lastname AS last,
      users.stu_number AS stuno,
      assignments.name AS test,
      rosters.p_id,
      preps.period AS period,
      preps.user

      FROM holder

      JOIN rosters
      ON rosters.stu_id = holder.student_id

      JOIN users
      ON users.id = holder.student_id

      JOIN assignments
      ON assignments.id = holder.t_id

      JOIN preps
      ON preps.id = rosters.p_id

      WHERE holder.visible = 0
      AND preps.user = 1
      AND assignments.user = 1
      AND holder.t_id = 1
      AND preps.period = 2
      ORDER BY score DESC
      ) x
 GROUP BY stuno
 ORDER BY last

你可以看到我注释掉的那条线是我试图让它显示一个" 0"如果为NULL,但它不起作用。我得到一份完整的清单,但是如果没有为学生找到分数,那该学生就不会出现在我的清单中。任何人都有解决方案/想法让我尝试?

我是否过度使用JOIN并让我的生活更加艰难?我大多是自学成才,所以我知道我在基础知识方面有一些漏洞。它并没有阻止我创造一些疯狂的酷项目......但是我时不时地确定我会给自己造成一些不必要的悲伤。

///////////////////////////////////////////////

以下是我对下面的答案所做的工作,以便从我的表格中获取信息:

SELECT au.stu_id, 
    COALESCE(t.id, 0) as id,
    COALESCE(t.score , 0) as score               
 FROM rosters au
 LEFT JOIN  (
    SELECT *
         FROM (
           SELECT a.*, 
              @rownum := if(@prev_value = student_id, 
              @rownum + 1, 
           1) rn,
         @prev_value := student_id as prev    
         FROM holder a, 
        (SELECT @rownum := 0, @prev_value := '') r
      ORDER BY student_id, score DESC
    ) T
  WHERE T.rn = 1) T  
ON au.stu_id = T.student_id

所以,这项工作很有效,除非它没有显示那些在给定考试中没有分数的学生。如果他们的得分没有在"持有人中找到"表,我希望它显示为" 0"。

/////////////////

等一下!我可能有错误...我认为它工作正常。我需要调整一些事情并回复你。顺便说一句,非常感谢您花时间帮助我!

1 个答案:

答案 0 :(得分:1)

你的第一个方法只能起作用,因为MySQL设计不好。

正确的方法应该是

<强> SQL Fiddle Demo

SELECT a.id, a.authorId, a.answer, a.votes
FROM (  SELECT authorId, 
               MAX(votes) as votes
        FROM answers
        GROUP BY authorId ) AS h
JOIN answers a
  ON a.authorId = h.authorId
 AND a.votes = h.votes;

<强>输出

| id | authorId | answer | votes |
|----|----------|--------|-------|
|  2 |        a |     x2 |    21 |
|  4 |        b |     x1 |    23 |  ==> 
|  5 |        b |     x2 |    23 |  ==> duplicates max value are possible

但如果几个答案有相同的分数,这就有问题。您需要包含一些逻辑来决定要显示哪一个。

您也可以使用变量来获得最高分。

SELECT *
FROM (
      SELECT a.*, 
            @rownum := if(@prev_value = authorId, 
                          @rownum + 1, 
                          1) rn,
            @prev_value := authorId as prev    
      FROM answers a, 
            (SELECT @rownum := 0, @prev_value := '') r
      ORDER BY authorId, votes DESC
     ) T
WHERE T.rn = 1;

<强>输出

| id | authorId | answer | votes | rn | prev |
|----|----------|--------|-------|----|------|
|  4 |        b |     x1 |    23 |  1 |    b | => only one is show but would      
|  2 |        a |     x2 |    21 |  1 |    a |    be random unless you specify some rule.

现在,对于您的问题,您还需要使用 LEFT JOIN 而不是JOIN来获得没有分数的学生。

像这样的东西

SELECT au.authorId, 
       COALESCE(t.id, 0) as id,
       COALESCE(t.answer , 0) as answer ,
       COALESCE(t.votes , 0) as votes               
FROM authors au
LEFT JOIN  (
            SELECT *
            FROM (
                  SELECT a.*, 
                        @rownum := if(@prev_value = authorId, 
                                      @rownum + 1, 
                                      1) rn,
                        @prev_value := authorId as prev    
                  FROM answers a, 
                        (SELECT @rownum := 0, @prev_value := '') r
                  ORDER BY authorId, votes DESC
                 ) T
            WHERE T.rn = 1) T  
      ON au.authorId = T.authorId

<强>输出

| authorId | id | answer | votes |
|----------|----|--------|-------|
|        a |  2 |     x2 |    21 |
|        b |  4 |     x1 |    23 |
|        c |  0 |      0 |     0 |