0

我有一个带有时间戳字段“bar”的表“foo”。如何仅获取查询的最旧时间戳,例如:SELECT foo.bar from foo?我尝试执行类似的操作: SELECT MIN(foo.bar) from foo 但因此错误而失败

第 1 行的错误 1140 (42000):如果没有 GROUP BY 子句,则混合没有 GROUP 列的 GROUP 列 (MIN(),MAX(),COUNT(),...) 是非法的

好的,所以我的查询比这复杂得多,这就是为什么我很难处理它。这是带有 MIN(a.timestamp) 的查询:

select distinct a.user_id as 'User ID',
       a.project_id as 'Remix Project Id',
       prjs.based_on_pid as 'Original Project ID',
       (case when f.reasons is NULL then 'N' else 'Y' end)
         as 'Flagged Y or N',
       f.reasons, f.timestamp, MIN(a.timestamp) 
from view_stats a
     join (select id, based_on_pid, user_id
                    from projects p) prjs on
     (a.project_id = prjs.id)
     left outer join flaggers f on
     (    f.project_id = a.project_id
      and f.user_id = a.user_id)
where a.project_id in
(select distinct b.id
   from projects b
  where b.based_on_pid in
                ( select distinct c.id
                    from projects c
                   where c.user_id = a.user_id
                )
)
order by f.reasons desc, a.user_id, a.project_id;

任何帮助将不胜感激。

view_stats 表:

+------------+------------------+------+-----+-------------------+----------------+
| Field      | Type             | Null | Key | Default           | Extra          |
+------------+------------------+------+-----+-------------------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL              | auto_increment | 
| user_id    | int(10) unsigned | NO   | MUL | 0                 |                | 
| project_id | int(10) unsigned | NO   | MUL | 0                 |                | 
| ipaddress  | bigint(20)       | YES  | MUL | NULL              |                | 
| timestamp  | timestamp        | NO   |     | CURRENT_TIMESTAMP |                | 
+------------+------------------+------+-----+-------------------+----------------+

更新

Based on thomas' suggestion I converted my query to:
select distinct a.user_id as 'User ID',
       a.project_id as 'Remix Project Id',
       prjs.based_on_pid as 'Original Project ID',
       (case when f.reasons is NULL then 'N' else 'Y' end)
         as 'Flagged Y or N',
       f.reasons, f.timestamp, min(a.timestamp)
from view_stats a
     join (select id, based_on_pid, user_id
                    from projects p) prjs on
     (a.project_id = prjs.id)
     left outer join flaggers f on
     (    f.project_id = a.project_id
      and f.user_id = a.user_id)
where a.project_id in
(select distinct b.id
   from projects b
  where b.based_on_pid in
                ( select distinct c.id
                    from projects c
                   where c.user_id = a.user_id
                )
)
group by a.project_id, a.user_id
order by a.timestamp
;

它现在正在运行。

4

1 回答 1

1

如果你打算使用聚合函数(如 min()、max()、avg() 等),你需要告诉数据库它究竟需要什么来获取 min()。

transaction    date
one            8/4/09
one            8/5/09
one            8/6/09
two            8/1/09
two            8/3/09
three          8/4/09

我假设您想要以下内容。

transaction    date
one            8/4/09
two            8/1/09
three          8/4/09

然后,您可以使用以下查询...注意 group by 子句,它告诉数据库如何对数据进行分组并获取某些东西的 min()。

select
    transaction,
    min(date)
from
    table
group by
    transaction
于 2009-08-06T19:14:32.850 回答