MYSQL
열 출력하기, 컬럼명 변경하기(as), 행 개수 제한 (limit)
soo15
2023. 5. 3. 23:59
1. 전체 열 출력하기
테이블의 전체 컬럼(column)을 모두 출력하고 싶을 때
SELECT 바로 다음에 *(별)을 작성한다.
SELECT *
FROM customers
출력 결과 (customers라는 테이블의 출력 결과)
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
: : |
: : |
: : |
: : |
: : |
: : |
: : |
2. 특정 열 출력하기
테이블 중 특정 컬럼(column)만 출력하고 싶을 때
SELECT 바로 다음에 원하는 컬럼(column) 이름을 콤마(,)로 구분하여 작성한다.
SELECT customerID, CustomerName
FROM customers
출력 결과
CustomerID | CustomerName |
1 | Alfreds Futterkiste |
2 | Ana Trujillo Emparedados y helados |
3 | Antonio Moreno Taquería |
4 | Around the Horn |
: : |
: : |
3. 칼럼명 변경하기
출력되는 컬럼명을 변경하고 싶을 때
컬럼 명 다음에 as를 작성하고 출력하고 싶은 컬럼명을 작성한다.
SELECT customerID as 고객ID, CustomerName as 고객이름
FROM customers
출력 결과
고객ID | 고객이름 |
1 | Alfreds Futterkiste |
2 | Ana Trujillo Emparedados y helados |
3 | Antonio Moreno Taquería |
4 | Around the Horn |
: : |
: : |
4. 행 개수 제한하기
출력되는 행의 개수를 제한하고 싶을 때
limit를 작성하여 제한한다.
SELECT customerID as 고객ID, CustomerName as 고객이름
FROM customers
LIMIT 3
출력 결과
고객ID | 고객이름 |
1 | Alfreds Futterkiste |
2 | Ana Trujillo Emparedados y helados |
3 | Antonio Moreno Taquería |
> LIMIT 3을 작성하였기 때문에 3개의 행만 출력된다.