1

私は2つのテーブルを持っています:

表 1 ( 「製品」とします):

----------------
| id | product |
----------------
|  1 | Apple   |
|  2 | Grape   |
|  3 | Orange  |

表 2 ( 「タグ」とします):

------------------------------
| id | product_id |    tag   |
------------------------------
|  1 |     1      |    tag1  |
|  2 |     1      |    tag2  |
|  3 |     2      |    tag2  |
|  4 |     2      |    tag3  |
|  5 |     3      |    tag4  |

結果としてそのようなテーブルを生成するSQLiteデータベースにリクエストを行いたい:

---------------------------
|   product  |    tags    |
---------------------------
|    Apple   | tag1, tag2 |
|    Grape   | tag2, tag3 |
|    Orange  |    tag4    |

どうすればこれを達成できますか?SQLite クエリ言語のみを使用してタグを 1 つの列に結合するにはどうすればよいですか?

4

1 に答える 1

1

あなたが探しているのは "group by" と "group_concat()" だと思います:

select products.product,group_concat(tags.tag) from products join tags on tags.product_id = products.rowid group by tags.product_id;
于 2012-07-17T07:17:11.760 に答える