我在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'
或
'Cancelled by PendingDepositCleanerJob. User didn't finish the payment process properly.'
我正在尝试提取CARD号码作为我查询中的另一列,此处应为:“ 489930 ****** 8888”,如果MISC列中未包含卡号,则为空。 提取此信息的最佳选择是什么?
答案 0 :(得分:1)
一些字符串操作
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次,或者数字格式不同(或仅是部分数字),它将无法正常工作
答案 1 :(得分:0)
MySQL支持regular expressions,我们可以将其用作最后的手段。
选择REGEXP_SUBSTR(misc,'489930 ****** 8888')为
CARD
返回的默认值将为null。希望这能使您满意。