MySQL将Group_Concat两个表放在一起

时间:2016-05-12 01:28:40

标签: mysql sql group-concat

我正在尝试按照我在x天内记录的所有价格将数据分组到表格中。我在使用SQL语句时遇到问题,这是我当前的声明

// get the total amount for each date
public static List<DateAmountList> GetTotalAmount()
{
    List<DateAmountList> results = new List<DateAmountList>();
    using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
    {
        results = conn.Query<DateAmountList>("SELECT Date, SUM(Amount) AS 'Amount' FROM DateAmountList GROUP BY Date");
    }
    return results;
}

这是我的product_prices表;

SELECT *, GROUP_CONCAT(Price) AS Prices FROM product_prices GROUP BY Product,Day

我希望价格按照这一天在1个表格中分组

+---------+--------------+
| Product | Day | Price  |
+---------+--------------+
|    Phone| 1   |  100   |
|  Speaker| 1   |  50    |
|   Phone | 2   |  230   |
|  Speaker| 2   |  80    |
+---------+--------------+

2 个答案:

答案 0 :(得分:1)

您必须删除Day子句中的GROUP BY。另外,将*替换为Product

SELECT
  Product, 
  GROUP_CONCAT(Price) AS Prices 
FROM product_prices 
GROUP BY Product

ONLINE DEMO

答案 1 :(得分:1)

如果要从最低到最高选择价格,则应在group_concat语句中使用order by

SELECT Product, 
GROUP_CONCAT(Price ORDER BY Price) AS Prices 
FROM product_prices 
GROUP BY Product

SQLFiddle