简体   繁体   English

如何使用SQL从格式化的字母数字字符串中提取第一和第二个数字?

[英]How to extract 1st and 2nd number from a formatted alphanumeric string using SQL?

Example data in a weight column (from 3 rows of data) weight列中的示例数据(来自3行数据)

+-------------------------+
| weight                  |
+-------------------------+
| Pump:398kg Motor:0kg    |
| Pump:448# Motor:997#    |
| Pump:355kg Motor:2495kg |
+-------------------------+

Is there a way to extract the first number from column, and the 2nd number in the column separately? 有没有办法分别从列中提取第一个数字和列中的第二个数字?

Note 注意

  • string will always start with Pump: 字符串将始终以Pump:开头Pump:
  • string Motor: will always be in the middle Motor:永远在中间
  • denomination delimiter is either # or kg , there are no others 面额定界符是#kg ,没有其他
  • weight is always an integer 重量始终是整数

My goal is to split the numbers each into their own column 我的目标是将数字分成各自的列

ie

UPDATE table set weight_pump = ?, weight_motor = ?
UPDATE table set weight_pump = 398, weight_motor = 0

I can solve this using PHP (SELECT each row, do REGEX to extract numbers, write back to the row) 我可以使用PHP解决此问题(选择每一行,执行REGEX提取数字,写回该行)

But what I am looking for is a pure SQL solution, if such exists. 但是,我正在寻找的是纯SQL解决方案(如果存在)。 Preferably a single SQL statement, if exists but I am not opposed to several statements or intermediate steps. 最好有一个SQL语句(如果存在),但我不反对几个语句或中间步骤。

I have seen other answers but they focus on extracting a single number from SQL, I am seeking something more powerful. 我看到了其他答案,但它们专注于从SQL提取单个数字,我正在寻找更强大的功能。

Note: it can be also done in two steps (one for "kg", one for "#") to simplify query. 注意:也可以分两个步骤完成(一个用于“ kg”,一个用于“#”)以简化查询。

Here's a solution, using substring_index(). 这是使用substring_index()的解决方案。

I like this function because it doesn't depend so much on specific character positions. 我喜欢这个功能,因为它并不太依赖特定的字符位置。

mysql> SELECT
  SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(weight, ':', -2), 'kg', 1), '#', 1) AS weight_pump,
  SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(weight, ':', -1), 'kg', 1), '#', 1) AS weight_motor 
FROM MyTable;

Output: 输出:

+-------------+--------------+
| weight_pump | weight_motor |
+-------------+--------------+
| 398         | 0            |
| 448         | 997          |
| 355         | 2495         |
+-------------+--------------+

This works with UPDATE too: 这也适用于UPDATE:

mysql> UPDATE MyTable
  SET weight_pump = SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(weight, ':', -2), 'kg', 1), '#', 1),
      weight_motor = SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(weight, ':', -1), 'kg', 1), '#', 1);

Read more about this function here: http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_substring-index 在此处阅读有关此功能的更多信息: http : //dev.mysql.com/doc/refman/5.7/zh-CN/string-functions.html#function_substring-index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL ORDER BY 第一列,但如果第一列为空,则用第二列中的值替换空值 - SQL ORDER BY 1st column, but if 1st column is empty replace the empty value by a value from 2nd column 在第一个表插入中更新第二个表的SQL语法 - SQL Syntax with Updating 2nd table on 1st table insert 使用第一条 SQL select 语句结果用于第二条 select 语句 - Using 1st SQL select statement results for 2nd select statement SQL:当第一个数据库中的表更新时,如何在第二个数据库中自动更新表? - SQL : How to update table Automatically in 2nd database when table in 1st database is updated? 如何在sql中比较同一表的第一条记录中的列A和第二条记录中的列B - How to compare columnA in 1st record and columnB in 2nd record of a same table in sql 是否可以使用PHP PDO从第一张表中的列从第二张表中获取数据? - Is it possible to get data from the 2nd table using column in 1st table using PHP PDO? MySQL使用第二表进行计算更新第一表 - MySQL update 1st Table using the 2nd Table with computation MySQL - 比较 2 个表并从第一个更新第二个 - MySQL - compare 2 tables and update the 2nd from the 1st 如何从第二个表中显示2个不同的ID从第一个表中显示ID - How to show id from 1st table from 2 different ids from 2nd table 第一个查询返回的第二个查询的结果 - Result from 2nd query returned in 1st query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM