我需要一些查询方面的帮助,我需要提取第一个表或第二个表中的数字。查询需要很快,因为我想要处理 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/