LoginSignup
1
0

More than 5 years have passed since last update.

【MySQL】ソートをした際のNULLの位置について

Last updated at Posted at 2018-12-13

MySQLでソートをした際のNULLの位置について

table

id name
1 aaa
2 NULL
3 bbb

昇順(NULLが上)

SELECT * FROM table ORDER BY name ASC;

実行結果:

id name
2 NULL
1 aaa
3 bbb

降順(NULLが下)

SELECT * FROM table ORDER BY name DESC;

実行結果:

id name
3 bbb
1 aaa
2 NULL

昇順(NULLが下)

SELECT * FROM table ORDER BY name IS NULL ASC;

実行結果:

id name
1 aaa
3 bbb
2 NULL

降順(NULLが上)

SELECT * FROM table ORDER BY name IS NULL DESC;

実行結果:

id name
2 NULL
3 bbb
1 aaa
1
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
1
0