myssql数据库表名转驼峰 - PLAY - ITeye博客
原文:myssql数据库表名转驼峰 - PLAY - ITeye博客
1.数据库的表名,字段命名一般用下划线分隔,在编程时一般使用的是驼峰命名
2.myssql没有INITCAP函数,以下写法支持四个下划线,可以自己扩展
说明:YOU_TABLE_NAME 替换为需要转驼峰的表名
YOU_TABLE_SCHEMA 替换为TABLE_SCHEMA
Sql代码
-- 变量赋值 select @tbl := 'YOU_TABLE_NAME',@scha := 'YOU_TABLE_SCHEMA'; -- 表名转驼峰且首字母大写 select CONCAT( -- _分割之后的第一段 UPPER(substr(SUBSTRING_INDEX(LOWER(t.TABLE_NAME),'_',1),1,1)), substr(SUBSTRING_INDEX(LOWER(t.TABLE_NAME),'_',1),2), -- _分割之后的第二段 substr(UPPER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',1)) + 2,1), substr(LOWER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',1)) + 3,length(SUBSTRING_INDEX(t.TABLE_NAME,'_',2)) - length(SUBSTRING_INDEX(t.TABLE_NAME,'_',1))-2), -- _分割之后的第三段 substr(UPPER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',2)) + 2,1), substr(LOWER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',2)) + 3,length(SUBSTRING_INDEX(t.TABLE_NAME,'_',3)) - length(SUBSTRING_INDEX(t.TABLE_NAME,'_',2))-2), -- _分割之后的第四段 substr(UPPER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',3)) + 2,1), substr(LOWER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',3)) + 3,length(SUBSTRING_INDEX(t.TABLE_NAME,'_',4)) - length(SUBSTRING_INDEX(t.TABLE_NAME,'_',3))-2), -- _分割之后的第五段 substr(UPPER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',4)) + 2,1), substr(LOWER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',4)) + 3,length(SUBSTRING_INDEX(t.TABLE_NAME,'_',5)) - length(SUBSTRING_INDEX(t.TABLE_NAME,'_',4))-2) ) from information_schema.`TABLES` t where t.TABLE_SCHEMA = @scha and t.TABLE_NAME = @tbl; -- 表名转驼峰且首字母小写 select CONCAT( -- _分割之后的第一段 SUBSTRING_INDEX(LOWER(t.TABLE_NAME),'_',1), -- _分割之后的第二段 substr(UPPER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',1)) + 2,1), substr(LOWER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',1)) + 3,length(SUBSTRING_INDEX(t.TABLE_NAME,'_',2)) - length(SUBSTRING_INDEX(t.TABLE_NAME,'_',1))-2), -- _分割之后的第三段 substr(UPPER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',2)) + 2,1), substr(LOWER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',2)) + 3,length(SUBSTRING_INDEX(t.TABLE_NAME,'_',3)) - length(SUBSTRING_INDEX(t.TABLE_NAME,'_',2))-2), -- _分割之后的第四段 substr(UPPER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',3)) + 2,1), substr(LOWER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',3)) + 3,length(SUBSTRING_INDEX(t.TABLE_NAME,'_',4)) - length(SUBSTRING_INDEX(t.TABLE_NAME,'_',3))-2), -- _分割之后的第五段 substr(UPPER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',4)) + 2,1), substr(LOWER(t.TABLE_NAME),length(SUBSTRING_INDEX(t.TABLE_NAME,'_',4)) + 3,length(SUBSTRING_INDEX(t.TABLE_NAME,'_',5)) - length(SUBSTRING_INDEX(t.TABLE_NAME,'_',4))-2) ) from information_schema.`TABLES` t where t.TABLE_SCHEMA = @scha and t.TABLE_NAME = @tbl;
posted on
2019-03-22 10:52 NET未来之路 阅读(
...) 评论(
...) 编辑 收藏