我有一个列,可以由空格分隔 1、2 或 3 个部分。 我需要提取每个部分“如果”它存在(字符串将始终包含至少第一部分)。
示例:
1: Apple
2: Apple Orange
3: Apple Pear lemon
|1| Apple | Apple | | |
|2| Apple Orange | Apple | Orange | |
|3| Pear lemon Orange | Pear | lemon | Orange |
我可以提取第一部分和第三部分 (但如果只有一个部分,这会在第三列中插入相同的值)。
SELECT
`productCode`,
SUBSTRING_INDEX( `productCode`, ' ', 1 ),
SUBSTRING_INDEX( `productCode`, ' ', -1 )
FROM tblSubmissionProducts;
最佳答案
您可以使用它来分割字符串:
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)]>
关于MySQL:如何将一个值分成 3 个可能的部分(除以空格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36841387/