我有MySQL数据库的表结构,如下所示:
表格数据:
Ref | Var | 3SAa combined % | 3SAa combined of | 3SAa Forward of |3SAa Forward % | 3SAa reverse % | 3SAa Forward of|
---------------------------------------------------------------------------------------------------------------------
AL23 TT 0.00 38.78 48.39 100.0 12.01 85.3
我想要的输出如下:
ID | Ref |Var |a combined % | a combined of | a Forward of |a Forward % | a reverse % | a Forward of|
--------------------------------------------------------------------------------------------------------
3SA AL23 TT 0.00 38.78 48.39 100.0 12.01 85.3
我想知道是否有任何方式可以使用MySQL。最大的问题是,我从未找到任何子串函数来截断列名。
答案 0 :(得分:0)
您可以在数据库上测试我的查询。您只需要设置前两个变量。 Tablename和包含字符串的行号。
self.b1
<强>测试强>
SET @table_name = 'mytab';
SET @col_id = 3;
SELECT DATABASE() INTO @dbname;
SELECT SUBSTRING_INDEX(COLUMN_NAME,' ' , 1) INTO @id_name
FROM information_schema.columns
WHERE table_schema = @dbname
AND TABLE_NAME = @table_name
AND ORDINAL_POSITION = @col_id;
SELECT CONCAT('SELECT \'', @id_name, '\' AS Id, ' , GROUP_CONCAT(
CONCAT( '`',COLUMN_NAME,'`')
, ' AS `', REPLACE(COLUMN_NAME,CONCAT(@id_name,' ') , '')
,'`\n' ), ' FROM ', @table_name) INTO @sql
FROM information_schema.columns
WHERE table_schema = @dbname
AND TABLE_NAME = @table_name
ORDER BY ORDINAL_POSITION;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
请告诉我它是否适合您