MySQL 将具有相同 ID 的多行值复制到新列中?

编程入门 行业动态 更新时间:2024-10-17 09:38:43
本文介绍了MySQL 将具有相同 ID 的多行值复制到新列中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 ItemID | File

1 /storage/somefile1.jpg 1 /storage/somefile2.jpg 1 /storage/somefile3.jpg 1 /storage/somefile5.jpg 2 /storage/somerandomfile.jpg 2 /storage/anotherrandomfile.jpg 2 /storage/yetanotherrandomfile.jpg 2 /storage/somefile.jpg

我想为每个文件创建一个新列,而不是每个文件有 1 行.如:

I am wanting to create a new column for each file rather than having 1 row per file. Such as:

ItemID | File | File2 | File3 etc...

1 /storage/somefile1.jpg | /storage/somefile2.jpg | /storage/.. 2 /storage/somerandomfile.jpg | /storage/anotherrandomfile.jpg | /storage/..

有什么办法可以通过查询自动执行此操作吗?

Is there any way to automate this with a query?

推荐答案

在看到您的评论并且您只是想将其制作成 CSV 后,您可以执行以下操作:

After seeing your comment and that you're just trying to make a CSV out of it, you can do something like this:

<?php $query = $db->query('SELECT ItemID, GROUP_CONCAT(File SEPARATOR \'|$|\') AS Files FROM Table GROUP BY ItemID'); // Use a string that cant appear as part of the filename as the separator $fh = fopen('items.csv', 'w'); foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) { $files = explode('|$|', $row['Files']); fputcsv($fh, array_merge(array($row['ItemID']), $files)); } fclose($fh);

更多推荐

MySQL 将具有相同 ID 的多行值复制到新列中?

本文发布于:2023-10-27 10:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533170.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:到新   MySQL   ID   多行值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!