将SQL行拆分为两行并正确构造

2024-09-28 20:47:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有大量的疑问大量的疑问,例如:

INSERT INTO `test` (`test_id`, `test_title`, `test_pic`, `test_date`, `test_date`, value) VALUES (10, 'Dance0', 'NoPhoto.jpg', '1900-01-01 00:00:00', 0), (11, 'Dance1', 'NoPhoto.jpg', '1900-01-01 00:00:00', 0), (12, 'Dance2', 'NoPhoto.jpg', '1900-01-01 00:00:00', 0), (13, 'Dance3', 'NoPhoto.jpg', '1900-01-01 00:00:00', 0)

我想拆分并添加id,然后关闭查询的括号(),):

(10, 'Dance0', 'NoPhoto.jpg'),
(10, '1900-01-01 00:00:00', 0),
(11, 'Dance1', 'NoPhoto.jpg'),
(11, '1900-01-01 00:00:00', 0), 
(12, 'Dance2', 'NoPhoto.jpg'),
(12, '1900-01-01 00:00:00', 0), 
(13, 'Dance', 'NoPhoto.jpg'),
(13, '1900-01-01 00:00:00', 0), 

如何做到这一点?如能提供该问题的示例/解决方案,将不胜感激

谢谢


Tags: testiddatetitlevaluejpginsertvalues
1条回答
网友
1楼 · 发布于 2024-09-28 20:47:19

最简单的方法是在加载数据后重新构造数据。为此:

select test_id, test_title, test_pic
from test
union all
select test_id, test_date, value
from test;

我不清楚你为什么要这样做。原始数据的格式似乎正确

编辑:

如果要创建两个表:

create table table1 as
    select test_id, test_title, test_pic
    from test;

create table table2 as
    select test_id, test_date, value
    from test;

相关问题 更多 >