我想做一个transpose / groupby。我解释一下我的情况。
我有一张看起来像这样的表
p_qualifier | p_value
-----------------------------------------------------|------------
|mooClassification/1.0/class-4.spherecorrectionlist |+0.75
|mooClassification/1.0/class-4.spherecorrectionlist |+1.00
|mooClassification/1.0/class-4.spherecorrectionlist |+1.25
|mooClassification/1.0/class-4.spherecorrectionlist |+1.50
|mooClassification/1.0/class-4.spherecorrectionlist |+1.75
我希望通过查询获得(但我不知道它是否可能)这样的事情
p_qualifier | p_value
-----------------------------------------------------|---------------------------------
|mooClassification/1.0/class-4.spherecorrectionlist |+0.75; +1.00; +1.25; +1.50; +1,75
感谢您的帮助。
答案 0 :(得分:1)
您正在寻找:
SELECT p_qualifier, GROUP_CONCAT(p_value SEPARATOR '; ')
FROM testtable GROUP BY p_qualifier
答案 1 :(得分:0)
您可以使用GROUP_CONCAT
:
SELECT p_qualifier, GROUP_CONCAT(p_value ORDER BY p_value ASC SEPARATOR '; ') AS `p_value`
FROM table
GROUP BY p_qualifier
它会将列p_qualifier
(按相同的行)分组,GROUP_CONCAT
会将整个GROUP
的结果集连接起来SEPARATOR
答案 2 :(得分:0)
如果您想对p_value
进行排序。然后在ORDER BY
内使用GROUP_CONCAT
子句。
查询
SELECT `p_qualifier`,
GROUP_CONCAT(`p_value`
ORDER BY CAST((RIGHT(`p_value`, LENGTH(`p_value`) - 1)) AS FLOAT) SEPARATOR '; ')
FROM `your_table_name`
GROUP BY `p_qualifier`;
希望p_value
是varchar。