【问题标题】:What am I doing wrong? Min(date), group by user我究竟做错了什么? Min(日期),按用户分组
【发布时间】:2011-09-22 23:58:34
【问题描述】:
SELECT min(date(`tx_date`)))) as start_date,
       `account_id` as 'id'
FROM my_table
group by id

这是返回每个tx_date,而不是分组并给我每个用户的最小值。我也为start_date 试过这个:from_days(min(to_days(date(tx_date))))

【问题讨论】:

    标签: mysql group-by min


    【解决方案1】:

    我认为您查询中的逻辑很好。您的表 my_table 是否有一个作为主键的 id 列?问题可能是查询是按表的id 列而不是您在查询中使用的id 别名进行分组的。

    如果您想在其 account_id 上进行 GROUP,请尝试以下操作:

    SELECT min(date(`tx_date`)) as start_date, `account_id` as 'id'
    FROM my_table
    group by `account_id`;
    

    我认为您的查询在 min(date(tx_date)) 周围有 2 个额外的右大括号,应该会导致错误。

    【讨论】:

      猜你喜欢
      • 2013-08-06
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 2019-12-23
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      相关资源
      最近更新 更多