0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ソート後のレコードの順番をSQLで取得する

Posted at

こちらは2019/2/8にQiita:Teamに投稿した記事をリライトしたものです。

やりたかったこと

ソート後のレコードの順番をSQLで取得する

どうしたか

SELECT 
  (SELECT count(*) + 1 
    FROM article
    WHERE created_at > article.created_at
    ORDER BY created_at DESC) AS displayOrder
FROM article
WHERE article.id = :id

ちょっと解説

  • 前提
    • 記事一覧は投稿日時(created_at)降順で並んでいる
  1. 取得したい記事より後に投稿された記事の件数を取得する
  2. (1の件数)+ 1で取得したい記事がcreated_at降順で並び替えた時の順番が取得できる

めちゃくちゃ単純な話でしたね。。。

EOF

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?