infoMore 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 |
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
What you can do with signing up