IT무새/Database

[MySQL] 데이터 정렬 | ORDER BY

코딩무새 2025. 1. 24. 00:57

껄껄껄

코딩무새입니다.

 

 


 

데이터를 조회해서 정렬하고 싶나요?

ORDER BY를 사용해서 정렬할 수 있습니다.

 

오름차순 정렬 ( Ascending )

데이터가 아래와 같이 있는데요.

username 기준 오름차순 정렬을 하고 싶네요.

SELECT id, username, email, created_at FROM users;
+----+----------+--------------------+---------------------+
| id | username | email              | created_at          |
+----+----------+--------------------+---------------------+
|  1 | parrot   | parrot@example.com | 2025-01-16 20:30:30 |
|  2 | test1    | test1@example.com  | 2025-01-16 20:39:50 |
|  3 | test2    | test2@example.com  | 2025-01-16 20:39:50 |
|  4 | test3    | test3@example.com  | 2025-01-16 20:39:50 |
|  5 | apt      | apt@rose.com       | 2025-01-20 00:43:39 |
|  6 | bear     | bear@jwt.com       | 2025-01-21 00:43:59 |
|  7 | zip      | zip@al.com         | 2025-01-24 00:44:35 |
+----+----------+--------------------+---------------------+
7 rows in set (0.00 sec)

 

정렬 방법입니다.

SELECT {COLUMN1}, {COLUMN2}, {COLUMN3}
FROM {TABLE_NAME}
ORDER BY {COLUMN3} ASC

 

ORDER BY 칼럼명 ASC 구문을 사용하여 오름차순 정렬을 할 수 있습니다.

ASC는 영어로 ascending의 약자입니다.

 

그럼 실행해 볼까요?

SELECT id, username, email, created_at 
FROM users 
ORDER BY username ASC;
+----+----------+--------------------+---------------------+
| id | username | email              | created_at          |
+----+----------+--------------------+---------------------+
|  5 | apt      | apt@rose.com       | 2025-01-20 00:43:39 |
|  6 | bear     | bear@jwt.com       | 2025-01-21 00:43:59 |
|  1 | parrot   | parrot@example.com | 2025-01-16 20:30:30 |
|  2 | test1    | test1@example.com  | 2025-01-16 20:39:50 |
|  3 | test2    | test2@example.com  | 2025-01-16 20:39:50 |
|  4 | test3    | test3@example.com  | 2025-01-16 20:39:50 |
|  7 | zip      | zip@al.com         | 2025-01-24 00:44:35 |
+----+----------+--------------------+---------------------+
7 rows in set (0.00 sec)

 

결과를 보시면 username 기준으로 잘 나온 것을 확인할 수 있습니다

 

내림차순 정렬 ( Descending )

내림차순 정렬은 ASC 대신 DESC를 써주면 됩니다.

DESC는 descending의 약자예요.

SELECT id, username, email, created_at 
FROM users 
ORDER BY username DESC;
+----+----------+--------------------+---------------------+
| id | username | email              | created_at          |
+----+----------+--------------------+---------------------+
|  7 | zip      | zip@al.com         | 2025-01-24 00:44:35 |
|  4 | test3    | test3@example.com  | 2025-01-16 20:39:50 |
|  3 | test2    | test2@example.com  | 2025-01-16 20:39:50 |
|  2 | test1    | test1@example.com  | 2025-01-16 20:39:50 |
|  1 | parrot   | parrot@example.com | 2025-01-16 20:30:30 |
|  6 | bear     | bear@jwt.com       | 2025-01-21 00:43:59 |
|  5 | apt      | apt@rose.com       | 2025-01-20 00:43:39 |
+----+----------+--------------------+---------------------+
7 rows in set (0.00 sec)

 

username 기준 내림차순 정렬이 된 것을 확인할 수 있습니다.

 

여러 정렬 기준

정렬조건이 여러 개면 어떻게 할까요?

예를 들어 create_at기준 오름차순을 하고 싶은데, 같은 시간대가 겹치면 다음 정렬 우선순위를 정해야 할 때.

이런 경우 말이죠.

", "로 연결해서 다음 칼럼을 추가해 주면 됩니다.

SELECT id, username, email, created_at 
FROM users 
ORDER BY created_at DESC, id ASC;
+----+----------+--------------------+---------------------+
| id | username | email              | created_at          |
+----+----------+--------------------+---------------------+
|  7 | zip      | zip@al.com         | 2025-01-24 00:44:35 |
|  6 | bear     | bear@jwt.com       | 2025-01-21 00:43:59 |
|  5 | apt      | apt@rose.com       | 2025-01-20 00:43:39 |
|  2 | test1    | test1@example.com  | 2025-01-16 20:39:50 |
|  3 | test2    | test2@example.com  | 2025-01-16 20:39:50 |
|  4 | test3    | test3@example.com  | 2025-01-16 20:39:50 |
|  1 | parrot   | parrot@example.com | 2025-01-16 20:30:30 |
+----+----------+--------------------+---------------------+
7 rows in set (0.00 sec)

 

위의 쿼리는 created_at 기준 내림차순 정렬, created_at이 같은 경우 id 기준 오름차순 정렬 조건으로 조회된 데이터예요.

id 2, 3, 4의 created_dt 일시가 동일하기  때문에  id 기준으로 오름차순 정렬이 된 것이 보입니다.

 

ASC / DESC를 뒤에 붙이지 않으면 기본적으로 오름차순 정렬이 되니 참고해 주세요.

 


 

정렬에 대해서 알아보았습니다.

껄껄껄