有没有办法只使用mysql将CamelCase转换为camel_case?我知道我可以在php中完成它,但我想能够在mysql中完成它,因为我需要转换数百万个字符串。
答案 0 :(得分:2)
这是一个解决方案:
delimiter $$
drop function if exists replaceCamelCase $$
create function replaceCamelCase( p_str TEXT ) returns text
BEGIN
declare v_pos int;
declare v_len int;
declare cnt int;
declare tmp text;
declare ret text;
set v_pos=1;
set v_len=1+char_length( p_str );
set ret = '';
set cnt = 0;
if p_str REGEXP('[^_]') then
while (v_pos<v_len)
do
set tmp = SUBSTR(p_str, v_pos, 1);
if tmp REGEXP BINARY '[A-Z]' = 1 then
if cnt > 0 then
set ret = concat(ret, '_');
end if;
set ret = concat(ret, lower(tmp));
set cnt = cnt + 1;
else
set ret = concat(ret, tmp);
end if;
set v_pos = v_pos + 1;
end while;
else
set ret = p_str;
end if;
RETURN ret;
end $$
用户喜欢这样:
SELECT replaceCamelCase(name) FROM `test` WHERE 1
输入/输出示例:
BlaTest
test_test
BaldieBal
TestStringCase
输出:
bla_test
test_test
baldie_bal
test_string_case
答案 1 :(得分:1)
自己敲门 - 但请看上面的评论:
SELECT REPLACE('CamelCase', BINARY 'C','_c');
+---------------------------------------+
| REPLACE('CamelCase', BINARY 'C','_c') |
+---------------------------------------+
| _camel_case |
+---------------------------------------+
答案 2 :(得分:-2)
您可以尝试以下代码: -
$result = strtolower(preg_replace('/(.)([A-Z])/', '$1_$2', $subject));
转换:
HelloKittyOlolo
Declaration
CrabCoreForefer
TestTest
要:
hello_kitty_ololo
declaration
crab_core_forefer
test_test
它可能对你有帮助。