Computer >> 컴퓨터 >  >> 프로그램 작성 >> MySQL

MySQL SELECT 결과에서 긴 텍스트 필드의 길이를 제한하시겠습니까?

<시간/>

MySQL의 SUBSTRING()을 사용하여 문자열 길이를 제한할 수 있습니다. 구문은 다음과 같습니다.

SELECT SUBSTRING(yourColumnName,1,yourIntegerValueToGetTheCharacters) as
anyVariableName from yourTableName;

위의 구문을 이해하기 위해 테이블을 생성해 보겠습니다. 테이블 생성 쿼리는 다음과 같습니다.

mysql> create table limitLengthOfLongTextDemo
   -> (
   -> sentence LONGTEXT
   -> );
Query OK, 0 rows affected (0.74 sec)

삽입 명령을 사용하여 테이블에 일부 레코드를 삽입하십시오. 쿼리는 다음과 같습니다.

mysql> insert into limitLengthOfLongTextDemo values('This is the introduction to MySQL');
Query OK, 1 row affected (0.17 sec)

mysql> insert into limitLengthOfLongTextDemo values('PL/SQL is the extension of Structured
Query Language');
Query OK, 1 row affected (0.19 sec)

mysql> insert into limitLengthOfLongTextDemo values('Java is an Object Oriented
Programming Language');
Query OK, 1 row affected (0.20 sec)

select 문을 사용하여 테이블의 모든 레코드를 표시합니다. 쿼리는 다음과 같습니다.

mysql> select *from limitLengthOfLongTextDemo;

다음은 출력입니다.

+------------------------------------------------------+
| sentence                                             |
+------------------------------------------------------+
| This is the introduction to MySQL                    |
| PL/SQL is the extension of Structured Query Language |
| Java is an Object Oriented Programming Language      |
+------------------------------------------------------+
3 rows in set (0.00 sec)

다음은 주어진 값의 문자를 가져오는 쿼리입니다.

mysql> select substring(sentence,1,26) as 26Characters from limitLengthOfLongTextDemo;

다음은 출력입니다.

+----------------------------+
| 26Characters               |
+----------------------------+
| This is the introduction t |
| PL/SQL is the extension of |
| Java is an Object Oriented |
+----------------------------+
3 rows in set (0.00 sec)