我搜索了又搜索,但我认为我遗漏了一些东西。我想要完成的是将 concat 的结果保存在一个表上,其中根据 ID 组合多行。
我的表只有 3 列: ID - Order_items_id - 自定义
ID 是唯一的,但从长远来看并不重要。
我想要完成的是将所有具有相同order_item_id
的行连接起来。 concat 仅适用于自定义
。
我的表格示例:
ID - order_item_id - customization
0 1231 19;1
1 1231 75;1
2 2345 10;5
我可以通过以下查询让结果以我想要的方式显示:
SELECT `order_item_id`, group_concat(`customizations` separator ';')
FROM `ojs_order_ing_export`
GROUP BY `order_item_id`
ID - order_item_id - customization
0 1231 19;1;75;1
2 2345 10;5
现在我有点困惑如何将此查询的结果插入到第三列中,或更新customization
列以保存连接查询的结果。
这样做的目标是将基于 order_item_id
的 customization
中的所有数据分组为一行,以便我可以将 customization
列与另一张 table 。
提前谢谢您。
最佳答案
我不确定我是否完全理解你的目标,但看起来你应该像 INSERT-SELECT
语句一样:
INSERT INTO my_new_table (order_item_id, order_item_customizations)
SELECT `order_item_id`, group_concat(`customizations` separator ';')
FROM `ojs_order_ing_export`
GROUP BY `order_item_id`
或者,例如
CREATE TABLE my_new_table
SELECT `order_item_id`, group_concat(`customizations` separator ';')
FROM `ojs_order_ing_export`
GROUP BY `order_item_id`
如果您想拥有相同的表但具有“合并”值 - 只需交换原始表和新表即可。
关于MYSQL - 根据ID插入多行连接结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25376538/