У меня есть структура таблицы в базе данных 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. Большая проблема, я никогда не нашел ни одной функции подстроки для обрезания имен столбцов.
Вы можете проверить мои запросы в вашей БД. Вам нужно только настроить первые два параметра. Имя таблицы и номер строки, которая содержит строку.
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;
Тестовое задание
MariaDB []> SELECT * FROM mytab;
+------+------+-----------------+------------------+
| Ref | Var | 3SAa combined % | 3SAa combined of |
+------+------+-----------------+------------------+
| AL23 | | 0.00 | 38.78 |
+------+------+-----------------+------------------+
1 row in set (0.00 sec)
MariaDB []> SET @table_name = 'mytab';
Query OK, 0 rows affected (0.00 sec)
MariaDB [bernd]> SET @col_id = 3;
Query OK, 0 rows affected (0.00 sec)
MariaDB []> SELECT DATABASE() INTO @dbname;
Query OK, 1 row affected (0.00 sec)
MariaDB []> 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;
Query OK, 1 row affected (0.01 sec)
MariaDB []> 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;
Query OK, 1 row affected (0.01 sec)
MariaDB []> PREPARE stmt FROM @sql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
MariaDB []> EXECUTE stmt;
+------+------+------+------------+-------------+
| Id | Ref | Var | combined % | combined of |
+------+------+------+------------+-------------+
| 3SAa | AL23 | | 0.00 | 38.78 |
+------+------+------+------------+-------------+
1 row in set (0.00 sec)
MariaDB []> DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0.00 sec)
Пожалуйста, дайте мне знать, если это работает для вас
Других решений пока нет …