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
Add a comment  | 

1 Answer 1

Reset to default 2

You 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