ID Model Color
1 BMW Red
2 BMW Green
3 BMW Yellow
4 BMW Black
5 Golf Red
6 Golf Green
7 Golf Yellow
8 Golf Black
我有汽车的颜色和颜色作为表格,我想把它作为一行。这将显示在c#
中的datagrid中Model Color1 Color2 Color3 Color4
BMW Red Green Yellow Black
Golf Red Green Yellow Black
- 编辑 我有一个有四种颜色的车型。我想让它显示在一行中。新名称列可以是任何ID,但ID可以更改。
所以它可能就像......
Model 1 2 3 4
BMW Red Green Yellow Black
Golf Red Green Yellow Black
答案 0 :(得分:1)
答案 1 :(得分:0)
SELECT model, group_concat(DISTINCT color)
FROM table
GROUP BY model
它将提供2个重新编码。型号名称和颜色模型的字符串。然后你需要在C#中处理以获得单独的颜色。
答案 2 :(得分:0)
我从:
开始SELECT COUNT(*), Model, Color
FROM Table
GROUP BY Model, Color
然后使用它返回的计数来构建你的表。
答案 3 :(得分:0)
SELECT *
FROM (SELECT Model, Color FROM TableName) as SourceT
PIVOT (MAX(Color) FOR Color IN ([Red], [Green], [Yellow], [Black])) as Pvt
答案 4 :(得分:0)
SELECT MODEL, [Red], [Green], [Yellow], [Black]
FROM (
SELECT model, color
FROM tblCartable
WHERE color IN ('Red', 'black', 'yellow', 'green')
) x
PIVOT first(model) FOR color IN ([Red], [Green], [Yellow], [Black])) p