admin管理员组文章数量:1123143
I have a table that contains astronomical image files in one column and astronomical objects in another column. There are images that may contain more than 1 object, so the table may look like this:
IMAGE | OBJECT |
---|---|
Picture1 | Obj1 |
Picture1 | Obj2 |
Picture1 | Obj3 |
Picture2 | Obj2 |
Picture2 | Obj4 |
I have a table that contains astronomical image files in one column and astronomical objects in another column. There are images that may contain more than 1 object, so the table may look like this:
IMAGE | OBJECT |
---|---|
Picture1 | Obj1 |
Picture1 | Obj2 |
Picture1 | Obj3 |
Picture2 | Obj2 |
Picture2 | Obj4 |
I would now like to generate a new table that has a "merged string" ob objects which may look like this:
IMAGE | OBJECTS |
---|---|
Picture1 | Obj1,Obj2,Obj3 |
Picture2 | Obj2,Obj4 |
Is this possible in SQL (I use SQLite)?
Share Improve this question edited 3 hours ago Dale K 27.1k15 gold badges55 silver badges82 bronze badges asked 5 hours ago Martin WeißMartin Weiß 91 silver badge4 bronze badges 1- GROUP_CONCAT() – Barmar Commented 5 hours ago
1 Answer
Reset to default 2You can group by the IMAGES using GROUP_CONCAT
.
SELECT
image,
GROUP_CONCAT(object) AS objects
FROM
test
GROUP BY
image;
Output
image | objects |
---|---|
Picture1 | Obj1,Obj2,Obj3 |
Picture2 | Obj2,Obj4 |
本文标签: sqlMerge columns values depending on another columnStack Overflow
版权声明:本文标题:sql - Merge columns values depending on another column - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736549752a1944496.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论