5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GROUP_CONCAT

Last updated at Posted at 2014-12-24

これは便利

| ID| 商品名| 出品者 |
|---+------+--------|
| 1 | 商品1 | ユーザ1 |
| 2 | 商品2 | ユーザ1 |
| 3 | 商品3 | ユーザ1 |
| 4 | 商品4 | ユーザ2 |
| 5 | 商品5 | ユーザ2 |
| 6 | 商品6 | ユーザ3 |

みたいなitemsテーブルがあったら

select "出品者", count(1) as c, group_concat("商品名") as "商品" from items group by "出品者";

| 出品者 | c | 商品 |
|--------+---+-----------------|
| ユーザ1 | 3 | 商品1,商品2,商品3 |
| ユーザ2 | 2 | 商品4,商品5 |
| ユーザ3 | 1 | 商品6 |

という集計結果が得られる

MySQLのgroup_concatで複数レコードを1行にまとめる - 文系プログラマによるTIPSブログ
http://treeapps.hatenablog.com/entry/20120530/p1

GROUP_CONCAT with limit
http://stackoverflow.com/questions/23608464/group-concat-with-limit

MySQLのGROUP_CONCATで途中で切れてしまう時の対処 | ちいさな創々
http://www.petitec.com/2011/07/mysql_group_concat_max_len/

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?