我有以下格式的MySQL表。这是我运行的程序的输出,我无法更改它。
+---+------------------------+
| | A B C D E |
+---+------------------------+
| | model amz wmt abt tgt |
| 1 | c3000 100 |
| 2 | c3000 200 |
| 3 | c3000 150 |
| 4 | c3000 125 |
| 5 | A1234 135 |
| 6 | A1234 105 |
+---+------------------------+
我想根据第1列中的值(即model)将所有行移动到一行中。需要注意的是,空行实际上并不是空白,而是包含空字符
期望的输出:
+---+-----------------------+
| | A B C D E |
+---+-----------------------+
| | model amz wmt abt tgt |
| 1 | c3000 100 200 150 125 |
| 2 | A1234 200 105 135 |
+---+-----------------------+
我尝试使用
select model,group_concat(wmt),group_concat(amz) from table_name group by model
我得到的输出充满了逗号
+---+----------------------------------+
| | A B |
+---+----------------------------------+
| | model amz wmt |
| 1 | c3000 ,,,,100,,,, ,,,200,,,, |
| 2 | A1234 ,,200,,,,,, ,105,,,,,, |
+---+----------------------------------+
答案 0 :(得分:1)
SELECT
model,
MIN(amz) AS amz,
MIN(wmt) AS wmt,
MIN(abt) AS abt,
MIN(tgt) AS tgt
FROM
table_name
GROUP BY
model
答案 1 :(得分:1)
您可以使用TRIM和IF将空值转换为空。
SELECT
model,
GROUP_CONCAT(IF(TRIM(wmt) = '', NULL, wmt)),
GROUP_CONCAT(IF(TRIM(amz) = '', NULL, amz))
FROM
table_name
GROUP BY model