我正在使用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


上一个查询返回以下结果集:

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


如您所见,返回的记录是重复的,我不需要重复。

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

我的问题是,尝试以这种方式进行操作(按牲畜种类_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返回以下错误消息:


  42000 SELECT列表的表达式#1不在GROUP BY子句中,并且包含未聚合的列'digital_services_DB.LS.id',该列在功能上不依赖于GROUP BY子句中的列;这与sql_mode = only_full_group_by不兼容


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

最佳答案

关于我在re中的评论:防止重复出现在第一位-我猜想LivestockDetails中有多行与同一个物种有关。但是,在您的查询中,您没有访问该表中的任何数据。

如果您只想报告在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


至少可以尽早停止复制)

08-18 11:35