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

문자열에 10개 이상의 단어가 있는 경우 점을 추가하는 MySQL 쿼리는 무엇입니까?


이를 위해 CASE 문을 사용합니다. 먼저 테이블을 생성하겠습니다 -

mysql> create table DemoTable
-> (
-> Title text
-> );
Query OK, 0 rows affected (0.61 sec)

삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -

mysql> insert into DemoTable values('My name is John and this is my first tutorial on MySQL');
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable values('This is Carol and I work on MongoDB');
Query OK, 1 row affected (0.19 sec)

select 문을 사용하여 테이블의 모든 레코드 표시 -

mysql> select *from DemoTable;

출력

이것은 다음과 같은 출력을 생성합니다 -

+--------------------------------------------------------+
| Title                                                  |
+--------------------------------------------------------+
| My name is John and this is my first tutorial on MySQL |
| This is Carol and I work on MongoDB                    |
+--------------------------------------------------------+
2 rows in set (0.00 sec)

다음은 문자열이 10단어 이상인 경우 점을 추가하는 쿼리입니다 -

mysql> select (case when substring_index(tbl.Title, ' ', 10) = tbl.Title then tbl.Title
-> else concat(substring_index(tbl.Title, ' ', 10), '....................')
-> end) AS AddDots
-> from DemoTable tbl;

출력

이것은 다음과 같은 출력을 생성합니다 -

+-------------------------------------------------------------------+
| AddDots                                                           |
+-------------------------------------------------------------------+
| My name is John and this is my first tutorial.................... |
| This is Carol and I work on MongoDB                               |
+-------------------------------------------------------------------+
2 rows in set (0.00 sec)