在MySQL中,可以使用SUBSTRING_INDEX函數來分割字符串。該函數的語法如下:
SUBSTRING_INDEX(str, delim, count)
例如,將"apple,orange,banana"分割成三個子字符串:
SELECT SUBSTRING_INDEX('apple,orange,banana', ',', 1) AS str1,
SUBSTRING_INDEX(SUBSTRING_INDEX('apple,orange,banana', ',', 2), ',', -1) AS str2,
SUBSTRING_INDEX('apple,orange,banana', ',', -1) AS str3;
輸出結果為:
+--------+--------+--------+
| str1 | str2 | str3 |
+--------+--------+--------+
| apple | orange | banana |
+--------+--------+--------+