为什么这个 GROUP BY 子句导致这个错误“42000Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column...”?

Why this GROUP BY clause cause this error "42000Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column..."?

我正在处理 MySql 数据库,并且正在执行此查询:

SELECT
    LS.id                                           AS livestock_species_id,
    LS.parent_livestock_species_id                  AS parent_livestock_species_id,
    LS.livestock_species_name_en                    AS livestock_species_name_en,  
    LSN.livestock_species_name                      AS livestock_species_name,
    LSN.description                                 AS description,
    LS.image_link                                   AS image_link

FROM LivestockDetails                               AS LD
INNER JOIN LivestockSpecies                         AS LS
      ON LD.live_stock_species_id = LS.id
LEFT JOIN LivestockSpeciesName                     AS LSN
      ON LSN.livestock_species_id = LS.id AND LSN.language_id = 3          

WHERE
     LD.ls_vaccination_id is not null  

上次查询return结果集如下:

livestock_species_id parent_livestock_species_id livestock_species_name_en                          livestock_species_name                             description image_link                                                                                                                                                                                                                                                     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1                                                Cow                                                Inka                                               Inka        https://firebasestorage.googleapis.com/v0/b/MY-PROJECT.appspot.com/o/img%2Ficons%2Flivestock%2Fcow.png?alt=media&token=c21866df-448a-4a72-9da2-d55d87f8b31c                                                                                
1                                                Cow                                                Inka                                               Inka        https://firebasestorage.googleapis.com/v0/b/MY-PROJECT.appspot.com/o/img%2Ficons%2Flivestock%2Fcow.png?alt=media&token=c21866df-448a-4a72-9da2-d55d87f8b31c                                                                                
1                                                Cow                                                Inka                                               Inka        https://firebasestorage.googleapis.com/v0/b/MY-PROJECT.appspot.com/o/img%2Ficons%2Flivestock%2Fcow.png?alt=media&token=c21866df-448a-4a72-9da2-d55d87f8b31c                                                                                
1                                                Cow                                                Inka                                               Inka        https://firebasestorage.googleapis.com/v0/b/MY-PROJECT.appspot.com/o/img%2Ficons%2Flivestock%2Fcow.png?alt=media&token=c21866df-448a-4a72-9da2-d55d87f8b31c                                                                                
2                                                Chicken                                            Inkoko                                             Inkoko      https://firebasestorage.googleapis.com/v0/b/MY-PROJECT.appspot.com/o/img%2Ficons%2Flivestock%2Fchicken.png?alt=media&token=badd0660-48ca-49f8-914a-8a9b8574a221                                                                            
2                                                Chicken                                            Inkoko                                             Inkoko      https://firebasestorage.googleapis.com/v0/b/MY-PROJECT.appspot.com/o/img%2Ficons%2Flivestock%2Fchicken.png?alt=media&token=badd0660-48ca-49f8-914a-8a9b8574a221                                                                            
1                                                Cow                                                Inka                                               Inka        https://firebasestorage.googleapis.com/v0/b/MY-PROJECT.appspot.com/o/img%2Ficons%2Flivestock%2Fcow.png?alt=media&token=c21866df-448a-4a72-9da2-d55d87f8b31c                                                                                
1                                                Cow                                                Inka                                               Inka        https://firebasestorage.googleapis.com/v0/b/MY-PROJECT.appspot.com/o/img%2Ficons%2Flivestock%2Fcow.png?alt=media&token=c21866df-448a-4a72-9da2-d55d87f8b31c                                                                                
2                                                Chicken                                            Inkoko                                             Inkoko      https://firebasestorage.googleapis.com/v0/b/MY-PROJECT.appspot.com/o/img%2Ficons%2Flivestock%2Fchicken.png?alt=media&token=badd0660-48ca-49f8-914a-8a9b8574a221                                                                            
2                                                Chicken                                            Inkoko                                             Inkoko      https://firebasestorage.googleapis.com/v0/b/MY-PROJECT.appspot.com/o/img%2Ficons%2Flivestock%2Fchicken.png?alt=media&token=badd0660-48ca-49f8-914a-8a9b8574a221            

如您所见,returned 记录是重复的,我不需要这些重复。

所以我的想法是使用 GROUP BY 子句。

我的问题是尝试这样做(按 livestock_species_id 检索字段分组):

SELECT
    LS.id                                           AS livestock_species_id,
    LS.parent_livestock_species_id                  AS parent_livestock_species_id,
    LS.livestock_species_name_en                    AS livestock_species_name_en,  
    LSN.livestock_species_name                      AS livestock_species_name,
    LSN.description                                 AS description,
    LS.image_link                                   AS image_link

FROM LivestockDetails                               AS LD
INNER JOIN LivestockSpecies                         AS LS
      ON LD.live_stock_species_id = LS.id
LEFT JOIN LivestockSpeciesName                     AS LSN
      ON LSN.livestock_species_id = LS.id AND LSN.language_id = 3          

WHERE
     LD.ls_vaccination_id is not null  

GROUP BY 
      livestock_species_id

MySql return出现以下错误信息:

42000Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'digital_services_DB.LS.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

为什么?怎么了?我错过了什么?我该如何解决这种情况,避免重复?

您需要指定分组中的所有列(或聚合它们)。在你的情况下,因为你有重复的记录,你可以这样做:

GROUP BY livestock_species_id,parent_livestock_species_id,
    livestock_species_name_en, livestock_species_name, 
    description,image_link

或者你可以使用不同的:

SELECT DISTINCT
    LS.id                                           AS livestock_species_id,
    LS.parent_livestock_species_id                  AS parent_livestock_species_id,
    LS.livestock_species_name_en                    AS livestock_species_name_en,  
    LSN.livestock_species_name                      AS livestock_species_name,
    LSN.description                                 AS description,
    LS.image_link                                   AS image_link

关于我在回复中的评论:首先防止重复项出现 - 我猜 LivestockDetails 中有多行与同一物种相关。然而,在您的查询中,您没有从 table.

访问任何数据

如果您只想报告在 LivestockDetails 中至少有一行的物种,请改用 EXISTS 检查:

SELECT
    LS.id                                           AS livestock_species_id,
    LS.parent_livestock_species_id                  AS parent_livestock_species_id,
    LS.livestock_species_name_en                    AS livestock_species_name_en,  
    LSN.livestock_species_name                      AS livestock_species_name,
    LSN.description                                 AS description,
    LS.image_link                                   AS image_link

FROM LivestockSpecies                         AS LS
LEFT JOIN LivestockSpeciesName                     AS LSN
      ON LSN.livestock_species_id = LS.id AND LSN.language_id = 3          
WHERE
     EXISTS (SELECT * from LivestockDetails LD
             WHERE LD.live_stock_species_id = LS.id
             and LD.ls_vaccination_id is not null)

这应该会产生更好的结果(如果优化器做得很好),因为我们一开始就不会生成重复项。

(如果存在检查的相关子查询工作不正常,您可能还想尝试:

SELECT
    LS.id                                           AS livestock_species_id,
    LS.parent_livestock_species_id                  AS parent_livestock_species_id,
    LS.livestock_species_name_en                    AS livestock_species_name_en,  
    LSN.livestock_species_name                      AS livestock_species_name,
    LSN.description                                 AS description,
    LS.image_link                                   AS image_link

FROM (SELECT DISTINCT live_stock_species_id
      FROM LivestockDetails
      WHERE ls_vaccination_id is not null)          AS LD
INNER JOIN LivestockSpecies                         AS LS
      ON LD.live_stock_species_id = LS.id
LEFT JOIN LivestockSpeciesName                     AS LSN
      ON LSN.livestock_species_id = LS.id AND LSN.language_id = 3

这至少会尽早停止复制)