私はタイムスタンプフィールド '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 | |
+------------+------------------+------+-----+-------------------+----------------+
を更新。
ありがとうございました。あなたのご意見は、私のクエリを再考するのに役立ちました。 – amh