MySQL:提取一个数字,该数字位于两个表之一中

标签 mysql

我需要一些查询方面的帮助,我需要提取第一个表或第二个表中的数字。查询需要很快,因为我想要处理 400 万个条目。

有两个表,看起来像这样:

subscriber
| id1 | pileofmud                  |
------------------------------------
| 111 | xxx,yyy,number:1110,zzz    |
| 112 | xxx,yyy,nothingthatmatters |
| 113 | xxx,yyy,nothingthatmatters |

subscriber_ext
| id2 | pileofmud                  |
------------------------------------
| 111 | xxx,yyy,nothingthatmatters |
| 112 | xxx,yyy,number:11200,zzz   |
| 113 | xxx,yyy,nothingthatmatters |

现在我想提取 id 和数字。 所以结果应该是:

| id  | num   |
---------------
| 111 | 1110  |
| 112 | 11200 |

id 113 不应出现在结果集中,因为它在“pileofmud”列中没有数字。

不幸的是,我在这里没有走得太远,但在伪代码中它应该看起来像这样:

SELECT id1 AS id, some_string_cutting_op AS num FROM subscriber     WHERE ID IN (111,112,113,...,4000000) UNION
SELECT id2 AS id, some_string_cutting_op AS num FROM subscriber_ext WHERE ID IN (111,112,113,...,4000000)

我的查询中的 UNION 不合适,我只是想给您一个想法。

顺便说一句:我想使用传统的字符串操作而不是正则表达式,因为这里速度很重要,应该为 4 个 mio 记录完成。

提前致谢。

最佳答案

你可以做你想做的事,但不会很快:

select *
from ((select s.id,
              substring_index(substring_index(pileofmud, 'number:', 2), 'number:', -1) + 0 as number
       from subscriber s
       where pileofmud like '%number:%'
      ) union all
      (select s.id2,
              substring_index(substring_index(pileofmud, 'number:', 2), 'number:', -1) + 0 as number
       from subscriber_ext s
       where pileofmud like '%number:%'
      ) 
     ) s;

关于MySQL:提取一个数字,该数字位于两个表之一中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33845084/

相关文章:

c# - 通过C#将创建过程命令上传到mysql时忽略@symbol

mysql - 同一列的多个值的 AND 子句

mysql - Laravel 获取带有参数绑定(bind)的 sql 查询

MySQL 查询多个值的计数

mysql - MYSQL 函数会减慢查询速度吗?

php - 即使此 php 或 sql 中没有报告错误,这也不会更新

php - 根据 mysql 字段值进行重定向

php - 如何在 CakePHP 中为一个模型动态使用多个数据库

mysql - 使用 MySQL 中的不同 View 从搜索和导出中获取相同的数据?

MYSQL 长 super 键