如何将一列的一个或多个值与GROUP BY组合



我以前也这样做过,但我忘了在我的应用程序中,我在哪里有这些代码。

我有以下数据,例如:

Room Type         Extras           Price
Deluxe            Extra Person     150
Super Deluxe      Extra Person     150
Deluxe            Breakfast        250
Super Deluxe      Breakfast        250
Junior Suite      Extra Person     200

现在我想使用mysql从这个表中返回一些东西,比如:

Room Type                     Extras           Price
Deluxe & Super Deluxe         Extra Person     150
Deluxe & Super Deluxe         Breakfast        250
Junior Suite                  Extra Person     200

这里的想法是将Extras和Price字段分组,并获得房型。

使用GROUP_CONCAT:

SELECT
    GROUP_CONCAT(`Room Type` SEPARATOR ' & ') Type AS `Room Type`,
    Extras,
    Price
FROM yourtable
GROUP BY Extras, Price

最新更新