解析字符串以从由下划线分隔的大字符串中获取数字
让我们首先创建一个数据表
mysql> create table DemoTable1961 ( Title text ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令向数据表中插入一些记录
mysql> insert into DemoTable1961 values('You_can_remove_the_string_part_only-10001-But_You_can_not_remove_the_numeric_parts'); Query OK, 1 row affected (0.00 sec)
使用 select 语句从数据表中显示所有记录
mysql> select * from DemoTable1961;
这会生成以下输出
+------------------------------------------------------------------------------------+ | Title | +------------------------------------------------------------------------------------+ | You_can_remove_the_string_part_only-10001-But_You_can_not_remove_the_numeric_parts | +------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
以下是对 MySQLi 查询以实现 CAST() 并从字符串中解析出数字
mysql> select cast(replace(replace('You_can_remove_the_string_part_only-10001-But_You_can_not_remove_the_numeric_parts','You_can_remove_the_string_part_only-',''), '-But_You_can_not_remove_the_numeric_parts','') as unsigned) as Output from DemoTable1961;
这会生成以下输出
+--------+ | Output | +--------+ | 10001 | +--------+ 1 row in set (0.00 sec)
广告