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

URL의 MySQL 문자열 마지막 인덱스?

<시간/>

마지막 인덱스를 얻으려면 MySQL의 SUBSTRING_INDEX() 함수를 사용하십시오. 구문은 다음과 같습니다 -

SELECT yourColumnName1,...N,SUBSTRING_INDEX(yourColumnName,’yourDelimiter’,-1)as anyVariableName from yourTableName;

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

mysql> create table LastIndexString
   -> (
   -> Id int,
   -> yourURL text
   -> );
Query OK, 0 rows affected (0.89 sec)

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

mysql> insert into LastIndexString values(1,'https −//www.example.com/home.html');
Query OK, 1 row affected (0.26 sec)

mysql> insert into LastIndexString values(2,'https −//exampledemo.example.com/index.jsp');
Query OK, 1 row affected (0.18 sec)

mysql> insert into LastIndexString values(3,'https −//www.example.com/question/LastString');
Query OK, 1 row affected (0.18 sec)

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

mysql> select *from LastIndexString;

다음은 URL 문자열을 표시하는 출력입니다 -

+------+---------------------------------------------+
| Id   | yourURL                                     |
+------+---------------------------------------------+
| 1    | https −//www.example.com/home.html          |
| 2    | https −//exampledemo.example.com/index.jsp  |
| 3    | https −//www.example.com/question/LastString|
+------+---------------------------------------------+
3 rows in set (0.00 sec)

다음은 URL 문자열에서 마지막 인덱스 문자열을 가져오는 쿼리입니다. -

mysql> select Id, substring_index(yourURL,'/',-1) as LastStringFromURL from LastIndexString;

다음은 URL의 일부를 표시하는 출력입니다 -

+------+-------------------+
| Id   | LastStringFromURL |
+------+-------------------+
|    1 | home.html         |
|    2 | index.jsp         |
|    3 | LastString        |
+------+-------------------+
3 rows in set (0.00 sec)