[英]MySQL Group_Concat two tables together
我正嘗試將我的數據按我在x天之內記錄的所有價格進行分組。 我在使用SQL語句時遇到問題,這是我當前的語句
SELECT *, GROUP_CONCAT(Price) AS Prices FROM product_prices GROUP BY Product,Day
這是我的product_prices表;
+---------+--------------+
| Product | Day | Price |
+---------+--------------+
| Phone| 1 | 100 |
| Speaker| 1 | 50 |
| Phone | 2 | 230 |
| Speaker| 2 | 80 |
+---------+--------------+
我想要這樣的價格在一天中將價格分組在一張桌子中
+---------+----------+
| Product | Prices |
+---------+----------+
| Phone| 100, 230 |
| Speaker| 50, 80 |
+---------+----------+
您必須在GROUP BY
子句中刪除Day
。 此外,將*
替換為Product
:
SELECT
Product,
GROUP_CONCAT(Price) AS Prices
FROM product_prices
GROUP BY Product
如果要從最低到最高選擇價格,則應在group_concat語句中使用order by
:
SELECT Product,
GROUP_CONCAT(Price ORDER BY Price) AS Prices
FROM product_prices
GROUP BY Product
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.