我在MySql服务器上运行的查询有问题。 这是查询:
SELECT itms.Gender,itms.Age, (CASE WHEN (plv.Discount = 0 OR t1.EverGreen = 1) THEN 'A' ELSE 'S' END) as SCode, itms.SubLineID, min(t1.DayDelivery)
FROM item as t1
INNER JOIN item_contest_summary as itms
on (t1.SeasonID=itms.SeasonID and t1.CollectionID=itms.CollectionID and t1.ItemID=itms.ItemID and itms.JoinType<2)
INNER JOIN pricelist_summary as plc
on (plc.CountryID = 'IT' AND plc.SeasonID = itms.PriceSeasonID AND plc.CollectionID = itms.PriceCollectionID AND plc.ItemID = itms.PriceItemID )
INNER JOIN pricelist AS plv
on (plv.ListCode = plc.ListCode AND plv.SeasonID = itms.PriceSeasonID AND plv.CollectionID = itms.PriceCollectionID AND plv.ItemID = itms.PriceItemID AND (plv.ListPrice > 0 or itms.IsGAG))
WHERE itms.SCode IN ('S', 'A') AND itms.Age in ('A','K','') AND t1.HasPrivateSale = 0 AND itms.ExclLista = 0 AND itms.IsGAG = 0
GROUP BY itms.Gender,itms.Age,
(CASE WHEN (plv.Discount = 0 OR t1.EverGreen = 1) THEN 'A' ELSE 'S' END),
itms.SubLineID
当我们在服务器上禁用严格的sql(在版本5.1中)时,该语句工作正常,然后我们将系统升级到5.6,启用严格的sql模式,这里出现问题。 这个查询的问题似乎是部分时的表达式:
(plv.Discount = 0 OR t1.EverGreen = 1)
在OR中只使用其中一个表达式,一切正常(两者都留下第一个和第二个表达式)。 似乎不可能在case when子句使用的表达式中使用任何AND / OR / etc。为什么? 有没有其他方法可以编写这种查询?
这是我得到的错误:
错误代码:1055。&#39; plv.Discount&#39;不在GROUP BY
答案 0 :(得分:0)
我发现使用别名进行分组非常有效。 因此,工作查询是:
SELECT itms.Gender,itms.Age,
(CASE WHEN (plv.Discount = 0 OR t1.EverGreen = 1) THEN 'A' ELSE 'S' END) as SCode,
itms.SubLineID , min(t1.DayDelivery)
FROM item as t1
INNER JOIN item_contest_summary as itms
on (t1.SeasonID=itms.SeasonID and t1.CollectionID=itms.CollectionID and t1.ItemID=itms.ItemID and itms.JoinType<2)
INNER JOIN pricelist_summary as plc
on (plc.CountryID = 'IT' AND plc.SeasonID = itms.PriceSeasonID AND plc.CollectionID = itms.PriceCollectionID AND plc.ItemID = itms.PriceItemID )
INNER JOIN pricelist AS plv
on (plv.ListCode = plc.ListCode AND plv.SeasonID = itms.PriceSeasonID AND plv.CollectionID = itms.PriceCollectionID AND plv.ItemID = itms.PriceItemID AND (plv.ListPrice > 0 or itms.IsGAG))
WHERE itms.SCode IN ('S', 'A') AND itms.Age in ('A','K','') AND t1.HasPrivateSale = 0 AND itms.ExclLista = 0 AND itms.IsGAG = 0
GROUP BY itms.Gender,itms.Age,
SCode,
itms.SubLineID
我仍然不明白为什么只有在 case 语句中使用复杂表达式时才需要这样做。 此外,即使没有选择上的聚合功能,此版本的查询仍然有效。 解决了这个问题,但仍然困惑......