mysql golang 根据表名转为struct,部分常用类型匹配,带注释。

-- mysql golang 根据表名转为struct,部分常用类型匹配,带注释。
select concat(
	UPPER(SUBSTRING(COLUMN_NAME,1,1)),
	SUBSTRING(COLUMN_NAME,2,length(COLUMN_NAME)),
	(case DATA_TYPE
		when 'varchar' then ' string '
		when 'int' then ' int '
		when 'double' then ' float32 '
		when 'float' then ' float64 '
		when 'datetime' then ' string '
		end),
		'`json:"',
		COLUMN_NAME,
		'"`',
		'// ',
		COLUMN_COMMENT
) as golang_variable
from (
	select 
        ORDINAL_POSITION,
	COLUMN_NAME,
	DATA_TYPE,
	COLUMN_COMMENT from information_schema.COLUMNS 
	where table_name = 'teacher' -- 填写所需要的表名
) a order by ORDINAL_POSITION
-- 支持mysql8.0+

posted @ 2019-06-04 16:56  只会玩卡尔  阅读(1152)  评论(2编辑  收藏  举报