【问题标题】:Locate text position, extract text and insert in new column in MySQL在 MySQL 的新列中定位文本位置,提取文本并插入
【发布时间】:2014-10-17 23:48:07
【问题描述】:

我在 MySQl 表中有以下行示例

              Column A    

Row1    Lauguage=English&Country=USA&Gender=Male
Row2    Gender=Female&Language=French&Country=
Row3    Country=Canada&Gender=&Language=English

我怎样才能做到以下几点:例如,我需要寻找国家

  1. 我需要在此文本列中定位 Country 的位置。这会因行而异。
  2. 然后我需要忽略参数“Country=”,只提取值。在某些情况下,这将是 NULL(例如 Row2 中的示例),而在某些行中,我需要后跟“=”的值(例如 Row1 和 Row3 中的示例)。但是,我需要确保我只获得价值。不是由'&'分隔的下一个参数
  3. 提取 Country 参数的值后,我需要创建一个新列,现在将在其中提取和存储这些值。

最终结果:新列

              Column B                                                

Row1            USA                            
Row2                              
Row3           Canada                                  

这里的任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 你为什么要把这样的数据放在你的表中?标准化!
  • 使用LOCATE()SUBSTRING() 函数。不幸的是,它在 SQL 中很复杂,因为您需要使用对 LOCATE() 的嵌套调用来找到结束它的 &

标签: mysql string return-value extract


【解决方案1】:

您可以选择“Country=”之后的文本,然后在获得该子字符串后,选择第一个“&”之前的文本

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(ColumnA, 'Country=', -1), '&', 1) AS ColumnB
FROM `atable`

http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_substring-index


这里有一个测试来演示:

mysql> SELECT * FROM atable;
+------+------------------------------------------+
| row  | columna                                  |
+------+------------------------------------------+
| Row1 | Lauguage=English&Country=USA&Gender=Male |
| Row2 | Gender=Female&Language=French&Country=   |
| Row3 | Country=Canada&Gender=&Language=English  |
+------+------------------------------------------+

mysql> SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(ColumnA, 'Country=', -1), '&', 1) AS ColumnB FROM atable;
+---------+
| ColumnB |
+---------+
| USA     |
|         |
| Canada  |
+---------+

关于您的后续问题:

INSERT INTO atable VALUES ('Row4', 'Gender=&Language=English');

SELECT `row`, IF(LOCATE('Country=', ColumnA)>0, 
  COALESCE(
    NULLIF(SUBSTRING_INDEX(SUBSTRING_INDEX(ColumnA, 'Country=', -1), '&', 1), ''), 
    'Blank string is not valid!'), 
 'Missing Country!') AS ColumnB     
FROM `atable`

+------+----------------------------+
| row  | ColumnB                    |
+------+----------------------------+
| Row1 | USA                        |
| Row2 | Blank string is not valid! |
| Row3 | Canada                     |
| Row4 | Missing Country!           |
+------+----------------------------+

【讨论】:

  • 谢谢,但这似乎仅适用于 Row3 中的示例。参数 Country= 的位置不一致。它可以是第 1 个参数,也可以是第 5 个参数。有没有办法找到 Country= 然后查找下一个 '&' 并获取要显示的值?在 excel 中尝试: 1. 存储在 A1 中的列数据 2. FIND("Country=",A1) -> (Position of Country) 存储在 B1 列中 3. IFERROR(FIND("="A1,B1),LEN(A1 ) -> (= 的位置) 存储在 C1 列 4. IFERROR(FIND("&",A1,C1),LEN(A1)+1) -> (结束位置) 存储在 D1 列 5. MID(A1 ,C1+1,D1-C1-1) -> 在单元格 E1 中显示 Country 值
  • @Nowitz41,我测试了它并包含了上面的输出。
  • 感谢您的输出。这正在工作!快速跟进,对于参数 Country= 为空白(例如: Country=&)且 Country= 不存在的行,是否也可以添加此错误检查功能?所以这只会更新 Country= 具有“有效”值的那些行。感谢您的帮助!
  • 感谢您的帮助!
  • @Nowitz41,太好了,我很乐意提供帮助。顺便说一句,在 StackOverflow 上习惯用赞成票或“接受的答案”来标记有用的答案。
【解决方案2】:

然而,我有完全相同的问题,对于任何想要 SQL 答案的人来说,在 SQL 中,请参阅 Tim Biegeleisen 评论 here

create table elb_logs(row varchar(100), url varchar(100));
insert into elb_logs values("Row1", "Lauguage=English&Country=USA&Gender=Male");

SELECT
    row,
    CASE WHEN POSITION('Country=' IN url) > 0
         THEN SPLIT_PART(SPLIT_PART(url, 'Country=', 2), '&', 1)
         ELSE 'Missing Country!' END AS ColumnB
FROM elb_logs;

【讨论】:

    猜你喜欢
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多