我甚至不知道如何用这个问题来搜索它。这是我的表:
我想把它放到MySQL中的一个表中,如下所示:
我不知道如何自动完成此操作。我使用的实际表格太大了,不能像这个例子那样手动操作。任何指导或帮助将不胜感激,谢谢!
答案 0 :(得分:0)
MySQL没有内置函数进行解包,因此您必须使用UNION ALL
:
SELECT 'A' AS Country, Date, A As 'Amount'
FROM yourTable
UNION ALL
SELECT 'B' AS Country, Date, B As 'Amount'
FROM yourTable
UNION ALL
SELECT 'C' AS Country, Date, C As 'Amount'
FROM yourTable
UNION ALL
SELECT 'D' AS Country, Date, D As 'Amount'
FROM yourTable
UNION ALL
SELECT 'E' AS Country, Date, E As 'Amount'
FROM yourTable
答案 1 :(得分:0)
SELECT 'SELECT ''' + [COLUMN_NAME] +''' as Country, Date, Amount
FROM yourTable
WHERE Country =''' + [COLUMN_NAME] +''' UNION'
FROM [INFORMATION_SCHEMA].[COLUMNS]
WHERE [TABLE_NAME] ='yourTable'
结果将是你想要的SQL。