你可以用它來分割字符串:
SELECT
REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX('This is a Test', ' ', 1)),' ',1)) AS n1,
REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX('This is a Test', ' ', 2)),' ',1)) AS n2,
REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX('This is a Test', ' ', 3)),' ',1)) AS n3,
REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX('This is a Test', ' ', 4)),' ',1)) AS n4;
樣品
MariaDB [(none)]> SELECT
-> REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX('This is a Test', ' ', 1)),' ',1)) AS n1,
-> REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX('This is a Test', ' ', 2)),' ',1)) AS n2,
-> REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX('This is a Test', ' ', 3)),' ',1)) AS n3,
-> REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX('This is a Test', ' ', 4)),' ',1)) AS n4;
+------+----+----+------+
| n1 | n2 | n3 | n4 |
+------+----+----+------+
| This | is | a | Test |
+------+----+----+------+
1 row in set (0.00 sec)
MariaDB [(none)]>
改變答案
SELECT
TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Apple', ' '), ' ', 1)),' ',1))) AS n1,
TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Apple', ' '), ' ', 2)),' ',1))) AS n2,
TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Apple', ' '), ' ', 3)),' ',1))) AS n1;
更好樣品
MariaDB [(none)]> SELECT
-> TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Apple', ' '), ' ', 1)),' ',1))) AS n1,
-> TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Apple', ' '), ' ', 2)),' ',1))) AS n2,
-> TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Apple', ' '), ' ', 3)),' ',1))) AS n1;
+-------+----+----+
| n1 | n2 | n1 |
+-------+----+----+
| Apple | | |
+-------+----+----+
1 row in set (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> SELECT
-> TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Apple Orange', ' '), ' ', 1)),' ',1))) AS n1,
-> TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Apple Orange', ' '), ' ', 2)),' ',1))) AS n2,
-> TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Apple Orange', ' '), ' ', 3)),' ',1))) AS n1;
+-------+--------+----+
| n1 | n2 | n1 |
+-------+--------+----+
| Apple | Orange | |
+-------+--------+----+
1 row in set (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> SELECT
-> TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Pear lemon Orange', ' '), ' ', 1)),' ',1))) AS n1,
-> TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Pear lemon Orange', ' '), ' ', 2)),' ',1))) AS n2,
-> TRIM(REVERSE(SUBSTRING_INDEX(REVERSE (SUBSTRING_INDEX(CONCAT('Pear lemon Orange', ' '), ' ', 3)),' ',1))) AS n1;
+------+-------+--------+
| n1 | n2 | n1 |
+------+-------+--------+
| Pear | lemon | Orange |
+------+-------+--------+
1 row in set (0.00 sec)
MariaDB [(none)]>
嗨貝恩德,這是偉大的! 但是有沒有辦法不復制字符串,如果只有一個部分? 所以在示例1中:Apple:所有列中都顯示相同的字符串: + -------- + ---------- + ------ + -------- + | n1 | n2 | n3 | n4 | + -------- + ---------- + ------ + -------- + |蘋果|蘋果|蘋果|蘋果| + -------- + ---------- + ------ + -------- + – nads
@nads - 如果在我的答案中添加了一個示例 –
這就是輝煌! 謝謝Bernd。這工作完美。 – nads