我有一个数据库表:
分隔每个请求的请求者 ID 列 (reqid)
类别 ID 列,将请求中的每个项目分为不同的类别 (catid)
项目 ID 列,用于分隔类别中的每个项目类型 (itemid)
项目名称列,列出项目(item)的名称
项目描述符列,提供项目描述 (desc)
金额列给出了该类别中每件商品的总成本(金额)
所以我的 table 看起来像这样:
|---------|-------|----------|--------|-------|------------|
| reqid | catid | itemid | item | desc | amount |
|---------|-------|----------|--------|-------|------------|
| 1 | 3 | 16 | food | food | 200 |
|---------|-------|----------|--------|-------|------------|
| 1 | 3 | 17 | water | wtr | 50 |
|---------|-------|----------|--------|-------|------------|
| 1 | 3 | 18 | film | film | 20 |
|---------|-------|----------|--------|-------|------------|
| 1 | 5 | 30 | room | room | 500 |
|---------|-------|----------|--------|-------|------------|
| 1 | 5 | 31 | chair | chair | 150 |
|---------|-------|----------|--------|-------|------------|
| 2 | 3 | 16 | food | food | 200 |
|---------|-------|----------|--------|-------|------------|
| 2 | 3 | 17 | water | wtr | 50 |
|---------|-------|----------|--------|-------|------------|
| 3 | 3 | 18 | film | film | 20 |
|---------|-------|----------|--------|-------|------------|
| 3 | 5 | 30 | room | room | 500 |
|---------|-------|----------|--------|-------|------------|
| 3 | 5 | 31 | chair | chair | 150 |
|---------|-------|----------|--------|-------|------------|
我希望我的查询结果看起来像
|--------|------------------|----------------------|--------------------|--------------------|
| reqid | catid3itemid | catid3item | catid3desc | catid3amount |
|--------|------------------|----------------------|--------------------|--------------------|
| 1 |16, 17, 18 | food, water, film | food, wtr, film | 200, 50, 20 |
|--------|------------------|----------------------|--------------------|--------------------|
| 2 |16, 17 | food, water |food, wtr | 200, 50, |
|--------|------------------|----------------------|--------------------|--------------------|
| 3 |18 | film | film | 20 |
|--------|------------------|----------------------|--------------------|--------------------|
继续:
|------------------|----------------------|--------------------|--------------------|
| catid4itemid | catid4item | catid4desc | catid4amount |
|------------------|----------------------|--------------------|--------------------|
| | | | |
|------------------|----------------------|--------------------|--------------------|
| | | | |
|------------------|----------------------|--------------------|--------------------|
| | | | |
|------------------|----------------------|--------------------|--------------------|
继续决赛:
|------------------|----------------------|--------------------|--------------------|
| catid5itemid | catid5item | catid5desc | catid5amount |
|------------------|----------------------|--------------------|--------------------|
|30,31 |room, chair |room, chair | 500, 150 |
|------------------|----------------------|--------------------|--------------------|
| | | | |
|------------------|----------------------|--------------------|--------------------|
|30,31 |room, chair |room, chair | 500, 150 |
|------------------|----------------------|--------------------|--------------------|
我一直在看到有关使用的帖子 GROUP_CONCAT 或 CROSS APPLY 或 SWITCH 语句
例如GROUP_CONCAT:
select *, GROUP_CONCAT(`table`.`categoryid` ORDER BY `table`.`categoryid` ASC SEPARATOR ', ') AS `categoryid`
from `table`
GROUP BY `table`.`requestid`
但这会将所有内容集中到一列中,不以类别 id 分隔,而是将所有类别 id 放在一列中。
任何建议或帮助将不胜感激。
最佳答案
谢谢大家的帮助!我在不使用数据透视表的情况下就弄清楚了
选择表
。requestid
AS request_id
,
group_concat((case when (table
.categoryid
= 2) then table
.item
end) 分隔符 ',' ) AS item2
,group_concat((case when (table
.categoryid
= 2) then table
.descriptor
end) 分隔符 ', ') AS 描述符2
,
group_concat((case when (table
.categoryid
= 2) then table
.amount
end) 分隔符 ',' ) AS 金额2
,
group_concat((case when (table
.categoryid
= 3) then table
.item
end) 分隔符 ',' )作为item3
,
group_concat((case when (table
.categoryid
= 3) then table
.descriptor
end) 分隔符 ',' ) AS 描述符3
,
group_concat((case when (table
.categoryid
= 3) then table
.amount
end) 分隔符 ',' ) AS 金额3
group_concat((case when (table
.categoryid
= 4) then table
.item
end) 分隔符 ',' )作为item4
,
group_concat((case when (table
.categoryid
= 4) then table
.descriptor
end) 分隔符 ',' ) AS 描述符4
,
group_concat((case when (table
.categoryid
= 4) then table
.amount
end) 分隔符 ',' ) AS 金额4
group_concat((case when (table
.categoryid
= 5) then table
.item
end) 分隔符 ',' )作为item5
,
group_concat((case when (table
.categoryid
= 5) then table
.descriptor
end) 分隔符 ',' ) AS 描述符5
,
group_concat((case when (table
.categoryid
= 5) then table
.amount
end) 分隔符 ',' ) AS amount5
来自表
其中 ((table
.categoryid
= 2) 或
(表
.categoryid
= 3) 或
(表
.categoryid
= 4) 或
(表
.categoryid
= 5))
按表
分组。requestid
关于MySQL - 1 id 多行并将多列数据转换为逗号分隔行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55031794/