帮助特定的MySQL查询:如何从一组多个值中获取最小值

时间:2009-08-06 19:04:00

标签: sql mysql mysql-error-1140

我有一个带有时间戳字段'bar'的表'foo'。如何获取查询的最旧时间戳,例如:来自foo的SELECT foo.bar?我试过做类似的事情:来自foo的SELECT MIN(foo.bar),但它失败并出现此错误

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

好的,所以我的查询比这复杂得多,这就是为什么我很难用它。这是使用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
;

现在正在运行。

1 个答案:

答案 0 :(得分: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