MySQL:Group By Min&最大价值

时间:2017-03-17 02:09:09

标签: mysql

我有以下查询

  

SELECT cped.entity_id,cped.value FROM sales_order so LEFT JOIN   sales_order_item soi ON so.entity_id = soi.order_id LEFT JOIN   catalog_product_entity_datetime cped ON soi.product_id =   cped.entity_id AND cped.attribute_id IN(153,154)

结果:

entity_id   value   
2340        2017-03-01 00:00:00
2340        2017-03-16 00:00:00
2312        2017-03-01 00:00:00
2312        2017-03-31 00:00:00

需要输出:

entity_id   value   
2340        2017-03-01 00:00:00 // Start Date
2340        2017-03-16 00:00:00 // End Date

所以entity_id明智的我们必须比较min&最大值与当前日期。它都小于当前日期,然后显示结果。我尝试使用但工作不正常。

1 个答案:

答案 0 :(得分:0)

如果您希望实体的日期在当前日期之前,则可以使用聚合:

SELECT cped.entity_id, GROUP_CONCAT(cped.value ORDER BY cped_value) as values
FROM sales_order so JOIN
     sales_order_item soi
     ON so.entity_id = soi.order_id JOIN
     catalog_product_entity_datetime cped
     ON soi.product_id = cped.entity_id AND
        cped.attribute_id IN (153,154)
GROUP BY cped.entity_id
HAVING  MAXcped_value) < CURDATE();  -- This condition is sufficient

这包括第二列,列表中包含值。

如果你真的想要原始行,那么你可以把它作为子查询并加入你想要的原始值。

相关问题