从字符串中提取特定值(MYSQL)

从字符串中提取特定值(MYSQL),mysql,Mysql,MYSQL表中的MISC列具有以下值: 'PrimeCC_Stripe/XX_582130/PMethod=VISA/CardType=VISA/489930******8888/12/2020/TraceId=7182992' 另一个例子: '-1/error/PMethod=VISA/CardType=VISA/489930******8888/12/2020/TraceId=714291' 或 我正在尝试将卡号提取为我查询中的另一列,这里应该是:“489930******8888”,如

MYSQL表中的MISC列具有以下值:

'PrimeCC_Stripe/XX_582130/PMethod=VISA/CardType=VISA/489930******8888/12/2020/TraceId=7182992'
另一个例子:

'-1/error/PMethod=VISA/CardType=VISA/489930******8888/12/2020/TraceId=714291'

我正在尝试将卡号提取为我查询中的另一列,这里应该是:“489930******8888”,如果MISC列中不包含卡号,则不包含任何内容。 提取此信息的最佳选项是什么?

MySQL支持,我们可以将其作为最后手段使用

选择REGEXP_SUBSTR(杂项,'489930****8888')作为


返回的默认值将为null。希望这能帮你分类。

一点字符串操作

drop table if exists t;
create table t (str varchar(100));
insert into t values
('PrimeCC_Stripe/XX_582130/PMethod=VISA/CardType=VISA/489930******8888/12/2020/TraceId=7182992'),
('Cancelled by PendingDepositCleanerJob. User didnt finish the payment process properly.'),
('123456******7891')
;

select str, 
        case when instr(str,'******') > 0 then
              concat(
              substring(str, instr(str,'******') - 6, 6),
              '******',
              substring(str, instr(str,'******') + 6, 4)
              )
        end
from t;

+----------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| PrimeCC_Stripe/XX_582130/PMethod=VISA/CardType=VISA/489930******8888/12/2020/TraceId=7182992 | 489930******8888                                                                                                                                                |
| Cancelled by PendingDepositCleanerJob. User didnt finish the payment process properly.       | NULL                                                                                                                                                            |
| 123456******7891                                                                             | 123456******7891                                                                                                                                                |
+----------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

但是,如果您的******发生次数超过1次,或者数字格式不同(或只是部分),则无法识别卡号。

如何识别卡号?6位+'******'+4位。如果存在“******”,那么它是一张卡片,我需要在星号前检索6位数字,在回答正确后检索4位数字。谢谢
drop table if exists t;
create table t (str varchar(100));
insert into t values
('PrimeCC_Stripe/XX_582130/PMethod=VISA/CardType=VISA/489930******8888/12/2020/TraceId=7182992'),
('Cancelled by PendingDepositCleanerJob. User didnt finish the payment process properly.'),
('123456******7891')
;

select str, 
        case when instr(str,'******') > 0 then
              concat(
              substring(str, instr(str,'******') - 6, 6),
              '******',
              substring(str, instr(str,'******') + 6, 4)
              )
        end
from t;

+----------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| PrimeCC_Stripe/XX_582130/PMethod=VISA/CardType=VISA/489930******8888/12/2020/TraceId=7182992 | 489930******8888                                                                                                                                                |
| Cancelled by PendingDepositCleanerJob. User didnt finish the payment process properly.       | NULL                                                                                                                                                            |
| 123456******7891                                                                             | 123456******7891                                                                                                                                                |
+----------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)