我有一个名为
的表顺序
| id | created | customer_id | ... | ... |
我想将创建的列和带有记录的customer_id等移动到名为
的第二个表中order_meta
| id | order_id | meta | value |
这样,元素"已创建" ,而值已创建值来自订单表
这可能与mysql查询有关,还是我需要为此创建一个脚本?
答案 0 :(得分:1)
这是你想要的吗?
insert into order_meta (order_id, meta, value)
insert id, 'created', created
from orders;
如果要对更多列进行非规范化,可以执行以下操作:
insert into order_meta (order_id, meta, value)
insert id, m.meta,
(case meta when 'created' then created
when 'customer_id' then customer_id
end)
from orders o cross join
(select 'created' as meta union all
select 'customer_id' as meta
) m
答案 1 :(得分:0)
INSERT INTO order_meta (order_id, meta, value)
SELECT id, 'created', created
FROM order