MySQL:如何将一个值分成 3 个可能的部分(除以空格)

标签 mysql substring

我有一个列,可以由空格分隔 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/

相关文章:

string - 如何从 Scala 中的字符串中删除子字符串/字符?

java - 将子字符串作为键并将以下子字符串作为值放入 TreeMap

c# - 从字符串请求中获取特定字符

mysql - 删除精确的重复记录(也具有相同的 id)但保留一条

mysql - 在博客文章中将 MySQL 表显示为 ASCII

Python:需要用额外的 HTML 或数据库结果替换 HTML 模板中一系列不同的子字符串

R:删除字符串中的子字符串

mysql - 如何在 SQL Where 条件操作符(> 或 <)中检查 1 个表列

php - 一个表格,一个按钮,一个mysql行

mysql - 通过从一张表中选择一列两次来连接mysql中的两个表