2009-08-06 7 views
0

私はタイムスタンプフィールド 'bar'を持つテーブル 'foo'を持っています。どのように私はクエリのような最も古いタイムスタンプだけを取得するのですか?SELECT foo.bar from foo?私はfooからSELECT MIN(foo.bar)を実行しようとしましたが、このエラーで失敗しました。特定のMySQLクエリのヘルプ:いくつかの値のグループから最小の値を得る方法

ERROR 1140(42000)at line 1:GROUP列(MIN()、MAX()、COUNT 、...)がGROUP BY句がない場合は無効です

OK、私のクエリはそれよりもはるかに複雑で、そのために苦労しています。これは、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テーブル:それは今実行している

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 
; 

+------------+------------------+------+-----+-------------------+----------------+ 
| 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 |    | 
+------------+------------------+------+-----+-------------------+----------------+ 

を更新。

答えて

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句に注意し、(分を取得します)。

select 
    transaction, 
    min(date) 
from 
    table 
group by 
    transaction 
+0

ありがとうございました。あなたのご意見は、私のクエリを再考するのに役立ちました。 – amh

関連する問題