1

重複の可能性:
列の最大値を持つ行を取得します

私のテーブルは次のようになります。

Sales 
- id
- country_id
- location_id
- order_id
- created

ここで、country_id と location_id のペアごとに 1 つの行 (最新のもの。最も高い ID 値を使用するか、作成された日時フィールドを使用することができます) のみが返されるようにします。

これは単一のクエリで可能ですか?

4

2 に答える 2

1
select * from (
  select * from mytable
  order by id desc) x
group by country_id, location_id;

これは mysql のみのソリューションですが、mon group-by 列を集計しない場合、mysql は各グループで最初に検出した行を提供し、グループ化する前に行を並べ替えると、2 つの行が得られるため、機能します。あなたがしたい。

于 2012-11-25T01:08:42.060 に答える
-1

これはただのはずです

select country_id, location_id, max(created)
from sales
group by country_id, location_id
于 2012-11-25T01:08:15.453 に答える