2016-04-25 65 views
0

我有一列可以有1,2或3個空格分開的部分。 我需要提取每個部分「如果」它存在(字符串將始終包含至少第一部分)。MySQL:如何將值分成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; 

回答

1

你可以用它來分割字符串:

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)]> 
+0

嗨貝恩德,這是偉大的! 但是有沒有辦法不復制字符串,如果只有一個部分? 所以在示例1中:Apple:所有列中都顯示相同的字符串: + -------- + ---------- + ------ + -------- + | n1 | n2 | n3 | n4 | + -------- + ---------- + ------ + -------- + |蘋果|蘋果|蘋果|蘋果| + -------- + ---------- + ------ + -------- + – nads

+0

@nads - 如果在我的答案中添加了一個示例 –

+0

這就是輝煌! 謝謝Bernd。這工作完美。 – nads

1

它應該工作

SELECT product_code, 
     SUBSTRING_INDEX(product_code, ' ',1), 
     SUBSTRING_INDEX(SUBSTRING_INDEX(product_code, ' ',2),' ',-1), 
     SUBSTRING_INDEX(product_code, ' ',-1) 
FROM tblSubmissionProducts 
相關問題