Computer >> 컴퓨터 >  >> 프로그래밍 >> MySQL

MySQL LEFT() 함수로 전체 값 중 200자만 표시하는 방법

MySQL에서 LEFT() 함수를 사용하면 컬럼의 전체 값 중 원하는 길이만큼의 문자만 잘라서 조회할 수 있습니다. 긴 텍스트 데이터를 목록 화면 등에 요약해서 보여줄 때 매우 유용한 함수입니다.

기본 문법

LEFT() 함수의 기본적인 사용 문법은 다음과 같습니다.

SELECT LEFT(yourColumnName, 200) AS anyAliasName FROM yourTableName;

LEFT(문자열, 길이) 형태로 작성하며, 두 번째 인자에 지정한 개수만큼 왼쪽(앞부분)부터 문자를 반환합니다.

예제 테이블 생성하기

먼저 실습에 사용할 테이블을 생성해 보겠습니다.

mysql> CREATE TABLE DemoTable (Paragraph LONGTEXT);
Query OK, 0 rows affected (0.71 sec)

레코드 삽입하기

INSERT 명령을 사용해 테이블에 샘플 데이터를 넣어 줍니다.

mysql> INSERT INTO DemoTable VALUES('Introduction to Java,Introduction to C,Introduction to C++,Introduction to Spring,Introduction to Hibernate,Introduction to Python,Introduction to MySQL,Introduction to MongoDB,Introduction to SQL Server,Introduction to ASP.net,Introduction to JSF');
Query OK, 1 row affected (0.13 sec)

저장된 레코드 확인하기

SELECT 명령으로 테이블에 저장된 전체 레코드를 조회합니다.

mysql> SELECT * FROM DemoTable;

실행 결과는 다음과 같습니다.

+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Paragraph |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Introduction to Java,Introduction to C,Introduction to C++,Introduction to Spring,Introduction to Hibernate,Introduction to Python,Introduction to MySQL,Introduction to MongoDB,Introduction to SQL Server,Introduction to ASP.net,Introduction to JSF |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

LEFT() 함수로 앞 200자만 표시하기

이제 전체 값 중 처음 200자만 표시하는 쿼리를 실행해 보겠습니다.

mysql> SELECT LEFT(Paragraph, 200) AS `200Characters` FROM DemoTable;

실행 결과, 전체 문자열이 아닌 앞에서 200자만 출력되는 것을 확인할 수 있습니다.

+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 200Characters |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Introduction to Java,Introduction to C,Introduction to C++,Introduction to Spring,Introduction to Hibernate,Introduction to Python,Introduction to MySQL,Introduction to MongoDB,Introduction to SQL Ser |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

추가로 알아두면 좋은 내용

  • 문자 단위 처리: MySQL의 LEFT() 함수는 바이트가 아닌 문자 단위로 동작하므로, utf8mb4 문자셋을 사용하는 환경에서도 한글이 포함된 문자열을 안전하게 자를 수 있습니다.
  • 뒤에서 자르기: 문자열 끝부분부터 일부만 가져오고 싶다면 RIGHT(컬럼명, 200) 함수를 사용하면 됩니다.
  • 잘림 표시하기: 값이 잘렸다는 점을 사용자에게 알리고 싶다면 CONCAT(LEFT(Paragraph, 200), '...')처럼 말줄임표를 붙여주는 방식도 좋습니다.

이처럼 LEFT() 함수 하나만으로도 긴 텍스트 데이터를 손쉽게 요약해서 표시할 수 있습니다.