【问题标题】:GROUP BY in mysql conditional on IS NULL columnmysql中的GROUP BY以IS NULL列为条件
【发布时间】:2018-05-25 02:46:32
【问题描述】:

我很难找出使用 GROUP BY 来消除这些结果中冗余行的正确方法:

id  title               author  city        year    pageId  page
1   The Faraway Tree    Blyton  New York    1951    NULL    1
1   The Faraway Tree    Blyton  New York    1951    NULL    2
1   The Faraway Tree    Blyton  New York    1951    NULL    3
1   The Faraway Tree    Blyton  New York    1951    NULL    4
1   The Faraway Tree    Blyton  New York    1951    NULL    5
1   The Faraway Tree    Blyton  New York    1951    NULL    7
1   The Faraway Tree    Blyton  New York    1951    NULL    9
1   The Faraway Tree    Blyton  New York    1951    NULL    13
1   The Faraway Tree    Blyton  New York    1951    NULL    30
2   Winnie the Pooh     Milne   London      1937    NULL    76
2   Winnie the Pooh     Milne   London      1937    NULL    73
2   Winnie the Pooh     Milne   London      1937    NULL    74
2   Winnie the Pooh     Milne   London      1937    NULL    75
1   The Faraway Tree    Blyton  New York    1951    4       32
2   Winnie the Pooh     Milne   London      1937    6       74

我想要做的只是消除重复,但前提是 pageId 为 NULL。

id  title               author  city        year    pageId  page
1   The Faraway Tree    Blyton  New York    1951    NULL    1
2   Winnie the Pooh     Milne   London      1937    NULL    76
1   The Faraway Tree    Blyton  New York    1951    4       32
2   Winnie the Pooh     Milne   London      1937    6       74

用简单的英语来说,逻辑是,如果 pageId 为 NULL,则按标题分组,否则将单行分开

这听起来很简单,但无论我使用什么聚合函数或 GROUP BY 修饰符,我都会收到语法错误或不正确的结果(通常是带有 pageId 的行与 NULL pageIds 分组)。

请注意,当 pageId 返回为 NULL 时,“page”列对我来说变得不重要了。

这是我正在使用的 SQL 查询(MySQL v. 5.6.37):

SELECT id,title,author,city,`year`,NULL as pageId,pageNum as page 
FROM titles
WHERE id IN ([complex subquery])
UNION ALL (
    SELECT id,title,author,city,`year`,pageId,pageNum 
    FROM titles 
    WHERE titles.t_id IN ([complex subquery])
    )

我已经解决了三天,在 SO 或 Web 上找不到准确的解决方案。我想是时候谦虚地请求你的帮助了。请。

【问题讨论】:

    标签: mysql group-by conditional


    【解决方案1】:

    如果你使用 IFNULL(pageId, 0) 然后在 grouped by 中使用这个伪列怎么办? 喜欢:
    SELECT id,title,author,city,year,IFNULL(pageId, 0) as pageId, pageNum as page FROM titles WHERE id IN ([complex subquery]) group by title, pageId;
    您可以在 IFNULL 中输入任何值而不是 0 - 'none' 也可以。

    【讨论】:

    • 这和 Yu-Lin 的回答都是我试图修复的东西。非常感谢您的意见:我很高兴知道我所做的事情并没有完全脱离基地。 Giorgos 的回答是所需要的。干杯!
    【解决方案2】:

    null pageId 转换为0,然后按它分组,有点棘手:

    select
        case when pageId is null then 0 else pageId end,
        id,title,author,city,`year`, page
    from titles
    group by 1, id,title,author,city,`year`, page
    

    【讨论】:

      【解决方案3】:

      这样的事情应该可以工作:

      SELECT id, title, author, city, `year`, NULL AS pageId, MIN(page) as page 
      FROM title
      WHERE pageId IS NULL
      GROUP BY id, title, author, city, `year`
      
      UNION ALL
      
      SELECT id, title, author, city, `year`, pageId, page 
      FROM title
      WHERE pageId IS NOT NULL;
      

      Demo here

      【讨论】:

      • 嗨,在第一个查询中,您可以按标题分组,正如@Parapluie 提到的那样
      • @backbone 我认为这是一个坏习惯。在最新版本的 MySQL 中,默认情况下这是禁用的。
      • @backbone 根据经验:SELECT 子句的每个非聚合字段都必须添加到 GROUP BY 子句中。在我们的例子中,这些字段是id, title, author, city, year
      • 好的,我现在正在进一步阅读。谢谢你的cmets :)。但无论如何,在他想要的结果的描述中,他只想按标题分组。因此,如果 2 行具有相同的标题并且 pageId 为空,但假设城市不一样,那么他只想要结果中的一行,而不是 2。那么您的长“分组依据”如何解决它?
      • 加油,乔治!有关我在做什么的解释:我试图将 GROUP BY 语句放在不正确的位置(在 UNION 之后)。把它放在第一个子查询中就可以了。桂冠归你。 谢谢。
      猜你喜欢
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      相关资源
      最近更新 更多