0

次の構造のテーブルがあります。

itemId  | direction | uid | created
133           0        17   1268497139
432           1        140  1268497423
133           0        17   1268498130
133           1        17   1268501451

2つの列に個別の値を選択する必要があるitemIdためdirection、出力は次のようになります。

itemId  | direction | uid | created
432           1        140  1268497423
133           0        17   1268498130
133           1        17   1268501451

元のテーブルには、itemId-133とdirection-0の2つの行がありますが、必要なのは、作成時刻が最新のこの行の1つだけです。

提案ありがとうございます!

4

2 に答える 2

1

使用する:

SELECT t.itemid,
       t.direction,
       t.uid,
       t.created
  FROM TABLE t
  JOIN (SELECT a.itemid,
               MAX(a.created) AS max_created
          FROM TABLE a
      GROUP BY a.itemid) b ON b.itemid = t.itemid
                          AND b.max_created = t.created

集計(IE:MAX)を使用して、itemidごとに作成された最大値を取得し、それをテーブルの変更されていないコピーに結合して、各itemidの作成された最大値に関連付けられた値を取得する必要があります。

于 2010-06-21T17:19:45.490 に答える
1
select t1.itemid, t1.direction, t1.uid, t1.created
from  (select t2.itemid, t2.direction, t2.created as maxdate 
       from tbl t2 
       group by itemid, direction) x
inner join tbl t1
  on  t1.itemid = x.itemid
  and t1.direction = x.direction
  and t1.created = x.maxdate
于 2010-06-21T17:20:15.447 に答える