0
0

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 3 years have passed since last update.

【SQL】ORDER BY, LIMIT

Last updated at Posted at 2021-05-22

はじめに

今回は、データの並び替えとデータ取得を制限する方法についてみていきたいと思います!

ORDER BY

昇順に並べたい場合は「ASC」、降順に並べたい場合は「DESC」と指定します。

以下のusersのテーブルを値段が小さい順に並べたい場合を考えます。

ScreenShot 2021-05-23 4.01.39.png

SELECT *
FROM users
ORDER BY price ASC;

ScreenShot 2021-05-23 4.05.53.png

LIMIT

最大何件のデータを取得するのかを指定することができます。
以下の表から、上から4件のみ取得したい場合を考えます。

ScreenShot 2021-05-23 4.09.16.png

SELECT *
FROM users
LIMIT 4;

ScreenShot 2021-05-23 4.10.11.png

このように、LIMITの後に取得したいデータ数を指定するだけで簡単に制限することができます。

おわりに

今回は、ORDER BYとLIMITについて学びました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?