I am using SQLite and have three tables (there is more data in these tables - abbreviated here):
- Categories - Holds item categories (id, name, description)
- Items - (id, name, status)
- Reference (id, cat_id, item_id)
Categories can have many items associated with it or none. Items must have at least 1 category and may be in more than 1 category.
Example:
(Categories)
| id | name | description |
|----|--------|---------------------------------|
| 1. | wet. | something associated with water |
| 2. | dry. | something else |
| 3. | metal. | steel, copper |
(Items)
| id. | name. | status |
|-----|---------|------------|
| 11. | river. | north fork |
| 12. | lake. | big |
| 13. | river. | south fork |
| 14. | desert. | mojave |
| 15. | car. | ford |
| 16. | truck. | chevy |
(Reference)
| id | cat_id. | item_id |
|----|---------|---------|
| 21 | 1 | 11 |
| 22 | 1 | 12 |
| 23 | 2 | 14 |
| 24 | 3 | 15 |
| 25 | 3 | 16 |
Using the following:
SELECT c.name,(i.name || "-" || i.status) as Related from Items as i
join Categories c where c.id = cat.id
I get something that looks like this:
| c.name | Related |
|---------|--------------------|
| wet | river - north fork |
| wet | lake - big |
| wet | river - south fork |
| dry | desert - mojave |
| metal | car - ford |
| metal | truck - chevy |
What I need is
| c.name | Related |
|--------|----------------------------------------------------|
| wet | river - north fork, lake - big, river - south fork |
| dry | desert - mojave |
| metal | car - ford, truck - chevy |
One category matched to the referenced items in the junction table and combined in the "Related" column (separated by comma in the example).
How do I get this result in SQLite?