我正在处理 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 子句。
我的问题是尝试以这种方式进行操作(按 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返回以下错误消息:
42000 SELECT列表的表达式#1不在GROUP BY子句中,并且包含非聚合列'digital_services_DB.LS.id',它在功能上不依赖于GROUP BY子句中的列;这与sql_mode = only_full_group_by
不兼容
为什么呢?怎么了?我错过了什么?如何解决这种情况,避免重复?
答案 0 :(得分:2)
您需要指定组中的所有列(或聚合它们)。在您的情况下,由于您有重复记录,您可以这样做:
GROUP BY livestock_species_id,parent_livestock_species_id,
livestock_species_name_en, livestock_species_name,
description,image_link
或者你可以使用distinct:
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
答案 1 :(得分:1)
关于我在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
至少可以尽早停止复制)