简体   繁体   English

MySQL GROUP BY与GROUP_CONCAT返回所有行合并?

[英]MySQL GROUP BY with GROUP_CONCAT return all row merged?

I ran into a problem today. 我今天遇到了一个问题。 I got a table with coins values : 我有一个硬币值表:

0.01
0.01
0.01
0.05
0.10
0.25
1.00
1.00
2.00
and so on...

So the main idea is to get all the value one time so 0.01, 0.05, etc.. 所以主要的想法是获得所有的价值一次0.01,0.05等。

So I do : 所以我这样做:

SELECT Valeur FROM Mint_Coins GROUP BY Valeur

Now it will give me one row for each value... I want all values in one row so I use this : 现在它将为每个值提供一行...我希望所有值都在一行中,所以我使用它:

SELECT GROUP_CONCAT(',', Valeur) AS Values FROM Mint_Coins GROUP BY Valeur

It give me again 7 rows in blob... to have blob it mean that the row are over 512 bytes... Okay, let's see when I convert that they contain... results is now : 它再次给我7行blob ...有blob它意味着行超过512字节...好吧,让我们看看我转换时它们包含...结果现在:

0.01, 0.01, 0.01, 0.01
0.05, 0.05
etc..

So what I am doing wrong ? 那么我做错了什么? I want the result to hold in one colomn and one row like this 0.01,0.05,0.10,0.25,1.00,2.00 . 我想要的结果在一个colomn和一排像这样持有0.01,0.05,0.10,0.25,1.00,2.00

Thanks 谢谢

You can pass distinct to group_concat to ignore duplicated values: 您可以将distinct传递给group_concat以忽略重复的值:

SELECT GROUP_CONCAT(DISTINCT Valeur) AS Values FROM Mint_Coins

You also don't need to group by in this case. 在这种情况下,您也不需要分组。

If you wanted the results in multiple rows, you could also have done: 如果您希望结果包含多行,您还可以完成:

SELECT DISTINCT Valeur FROM Mint_Coins
SELECT GROUP_CONCAT(A.Valuer SEPARATOR ',') AS Valuer
FROM (SELECT DISTINCT Valuer FROM Mint_Coins)A ORDER BY Valuer;

Check this valuable link.This will help you. 检查这个有价值的链接。这将对您有所帮助。

http://mahmudahsan.wordpress.com/2008/08/27/mysql-the-group_concat-function/ http://mahmudahsan.wordpress.com/2008/08/27/mysql-the-group_concat-function/

你能使用嵌套的子查询吗?

SELECT GROUP_CONCAT(',', (SELECT Valeur FROM Mint_Coins GROUP BY Valeur)) AS Values FROM Mint_Coins

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM